cron job question (for checking kernel updates)

Rick Stevens ricks at alldigital.com
Wed Oct 2 16:26:32 UTC 2013


On 10/02/2013 08:10 AM, Ranjan Maitra issued this missive:
> Hi,
>
> I have a cron job running which yum updates all my machines once a day.
> All of these work fine.
>
> I also have a cron job which checks for kernel updates every hour and
> sends me a message if an updated kernel has been installed. Here is the
> relevant script:
>
> --- begin file called check-kernel.sh in my scripts/yum directory---
>
> #!/bin/bash
>
> latestkernel=$(rpm -q kernel |tail -n1|sed -e 's/kernel-//')
>
> #echo "$latestkernel"
>
> if uname -a | grep -qv "$latestkernel"; then
> notify-send "Kernel UPDATE on ${HOSTNAME}: Running Kernel is $(uname
> -r) but lat est installed rpm is ${latestkernel}; REBOOT required"
> fi;
>
>
> --- end file called check-kernel.sh ---
>
> (A bit wordy, I know.)
>
> I have verified that this is executable and works from the commandline
> without any errors.
>
> I have the following set up via crontab -e:
>
> 0,15,30,45 * * * * nice -n 19 $HOME/.xplanet/download_xplanet_cloudmap
> 5 * * * * nice -n 19 $HOME/scripts/yum/check-kernel.sh
>
> The first line downloads cloudmaps for use with xplanet and works just
> fine (or appears to) and has done so for aeons.
>
> However, nothing happens (or appears to) for the second line.
>
> What is wrong here?

The most common problem I've seen is that cron jobs typically have very
limited paths and the executables you're running (notify-send, uname)
may not be in the path the cronjob has. For giggles, do "echo $PATH" as
the user the cronjob will run as. Then build a cron job that echos
cron's concept of the path:

	#!/bin/bash
	echo $PATH >/tmp/pathecho.txt

and have cron run that once as the same user. Look at the data in
/tmp/pathecho.txt and I'll bet you'll find its far more restricted than
that of an interactive shell. To fix it, put in a

	PATH="data from the echo $PATH command"

right after the "#!/bin/bash" line in your cron job. Since most of
your commands are in /bin, you could do:

	PATH=$PATH:/bin

and that would probably suffice (just making sure /bin is in the cron's
path).
----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, AllDigital    ricks at alldigital.com -
- AIM/Skype: therps2        ICQ: 22643734            Yahoo: origrps2 -
-                                                                    -
- Grabel's Law: 2 is not equal to 3--not even for large values of 2. -
----------------------------------------------------------------------


More information about the users mailing list