Andrew Beresford's Blog

Cron’s environment is not your environment

without comments

Ok… a work related post.

I’ve been doing some work on systems that run commands automatically with Cron and our developers have been hitting some problems with commands that run fine as root, but don’t work correctly when they are executed by cron.

The cause of this is differences in the environment between the interactive root user environment and the cron root user environment. Let me demonstrate;

root@eeyore:~# env | wc -l
21

My interactive root user environment 21 lines of “stuff” in it, but if I run a env under cron and output that to a file I get this;

root@eeyore:~# wc -l /tmp/crontmp
5 /tmp/crontmp

Two quite different values.

The interactive $PATH is;

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/usr/X11R6/bin

whilst the cron PATH is;

/usr/bin:/bin

Hence you will have difficulties finding tools outside of this rather limited path. Quite often things like Java are outside of the limited path.

If you want to have a more complete PATH, you’ll either have to define on the individual lines in your crontab, or at the top of the crontab entry.

Update

The limited crontab is defined in /etc/crontab. On a RHEL4 system it defines;


SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

Written by beezly

August 20th, 2008 at 3:32 pm

Posted in work

Tagged with

Leave a Reply