Search code examples
linuxbashcron

I have a crontab entry that calls a bash script. Why does $USER return blank?


In the monitor shell script, $USER returns blank when run from a user's crontab? However, $USER returns "experiment" when the script is run from the command line.

# CRON: execute monitor script every minute on user:experiment
*/1 * * * * /home/experiment/monitor

Solution

  • As suggested in the comments, cron uses a rather minimal environment, with most of the environment variables undefined, and a very short $PATH. In many versions of crontab, you can define the variables you need at the top of the crontab file, like so:

    USER=foo
    BAR=baz
    PATH=/bletch:/bazoo:"$PATH"