[Solved] grep and cron & command line
On Tue, 18 Mar 2008 10:54:46 -0700, Bob McGowan wrote:
>>> Now the crontab reads:
>>>
>>> * * * * * root is_burning || logger get executed.
>>>
>>> + set -x
>>> + ps -eaf
>>> + grep -E -[dts]ao |cdrdao *write|growisofs.*speed='
>>> root 15306 15295 0 09:29 ? 00:00:00 grep -E cdrecord.*
>>> -[dts]ao |cdrdao *write|growisofs.*speed=
>>> + exit 0
>>>
>>> $ is_burning || echo not burning CD/DVD
>>> not burning CD/DVD
>>>
>>> I.e., having put the 'ps | grep' part into a shell script, the
>>> behavior is still the same.
>>> Does it has anything to do with busybox?
>
> If I followed the above discussion correctly, the OP's concern is that
> sometimes the grep will return a result, even when the specified cd
> related programs are not running, and that this happens when the grep is
> run from cron but not from the command line.
Exactly. Right ON!
> If this is correct, the results you're seeing can in fact happen in
> either case. Whether you see a result or not is totally dependent on
> the process scheduler, timing of execution slices and perhaps your
> processor speed and/or if it's an SMP environment.
>
> What's happening is the grep you're running is sometimes finding itself.
> For example:
>
> ps -ef|grep firefox|cut -c1-60
> rmcgowan 17073 16974 0 08:13 ? 00:00:00 /bin/sh /usr
> rmcgowan 17080 17073 0 08:13 ? 00:00:00 /bin/sh /usr
> rmcgowan 17106 17080 0 08:13 ? 00:00:59 /usr/local/f
> rmcgowan 30742 17130 0 10:49 pts/1 00:00:00 grep firefox
>
> The last line is the grep that ran. So, one line is sometimes returned,
> even when firefox is not running.
>
> There are a number of ways to get around the problem. My favorite is to
> use something that will show up in the ps output for the grep itself but
> which will not be part of what you're looking for. But this does
> require using either the -l or -f options (which it looks like you are).
>
> ps -ef | grep '[f]irefox'
>
> will eliminate the grep command itself because the pattern can never
> match itself. . .
>
> I hope this helps solve your problem.
Bingo!!!
That's exactly the reason. As you can see from my script that I've already
applied the '[]' trick, but the trick wasn't applied to all 3 grep cases.
Having added '[]' to each of the grep cases, I finally see:
Mar 18 21:29:01 cxmr logger: get executed.
Bravo!!!
Thanks a lot to everyone who replied!
--
Tong (remove underscore(s) to reply)
http://xpt.sourceforge.net/techdocs/
http://xpt.sourceforge.net/tools/
Reply to: