Subscribe to
Posts
Comments
NSLog(); Header Image

Mysterious Cron Issue

This comes from a friend of mine in need of some help. I'll reproduce what he emailed me as best as I can (in terms of presentation, I mean):

I created a simple shell script to kill a running process (MSN Messenger) by name. When run independantly it works without issue. When run via crontab the lines with kill commands do not run. I'm sure it's running because the log file is updated at the time of execution. I've tried running is via root's crontab as well as an administrator's crontab with no luck. Any idea as to what might be supressing the kill statements?

MSN Killer Shell Script:

#!/bin/bash
kill `/bin/ps -auxc | /usr/bin/awk '/MSN Messenger Daemon/ {print $2}'`
kill `/bin/ps -auxc | /usr/bin/awk '/MSN Messenger/ {print $2}'`
/bin/echo "MSN Killed: `date`" >> /msn_killer.log

Crontab:

SHELL=/bin/sh
PATH=/bin
HOME=/var/log
*    *    *    *    *    /.msn_killer

For whatever reason, the kills never run. When run via "/.msn_killer" it works fine. When the command is run within a bash prompt, it works fine. Within crontab, it fails to work.

Note: My friend will check this post occasionally, and may be able to post follow-up answers to any questions y'all may have.

10 Responses to "Mysterious Cron Issue"

  1. specify the full path to kill -- /bin/kill.

    better yet, instead of the kludgy ps and awk crap, just use killall.

    i.e. /usr/bin/killall 'MSN Messenger'

  2. ps uses the size of the terminal to determine how much information to print to the screen. The standard terminal is 80x25. If I do ps -augx in a standard monitor, I see there is no way the entire string "MSN Messenger" will fit, so awk won't be able to find it.

    Try truncating the process name to 12 characters and you might have more luck...

  3. Paul is right. When scripts are run by cron their environment variables are pretty minimal.

    This means that what's normally in your $PATH won't be there when it's run via cron.

    You can either specify the full path to the programs your using in your script or set the $PATH variable within the script so it can find the binaries/programs you need.

    HTH.

  4. Adding the full path to kill made no difference and for whatever reason MSN Messenger (and the MSN Daemon) is unaffected by killall.

    As for the ps / terminal window size, does it really make a difference? I know if I run ps directly it will be truncated but isn't all ps data passed when piped?

    Thanks so much for the help!

  5. Okay, here is the wired thing.

    ps -augx | awk '/Mail/'

    works fine when the terminal size is too small to display the last column, or when the terminal size is large enough to display the full process name.

    However, if the terminal size is such that it will only show, say, 3 letters of the process name, it fails.

    Try this yourself. Set the terminal size to 72x24 (don't forget to use "resize" so that the terminal size is updated). Try it out with a process you know is there. See if it breaks. It does for me...

  6. Try using the -w option twice for ps, like this:

    ps -wwauxc

    Here's the relevant part from the manpage:

    -w Use 132 columns to display information, instead of the default

    which is your window size. If the -w option is specified more

    than once, ps will use as many columns as necessary without

    regard for your window size.

  7. When run as a shell script the terminal window width does not affect the functionality of the script. I ran it at 6x6 and it worked just fine.

  8. The ww flags DID make it work via cron! Thanks a million Olivier!

  9. one other note: ps doesn't need a hyphen. i.e.

    $ ps axwww

    works fine.

  10. Hi fellas,

    I have a similar problem...

    I am using ps -ef. The script works fine if directly run. However it doesn't work through cron.

    The ww flags do not help. Also, I am using the full paths for all the progs.

    Any suggestions?