1

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
fpmurphy
  • 2,464
  • 1
  • 18
  • 22
jdl
  • 6,151
  • 19
  • 83
  • 132
  • 2
    I think you'll find that most of the environment variables are not set. Run a crontab job where you log it `env > /tmp/env.log` If you want some variables set you can do that at the top of the crontab file (in most implementations). – Ted Lyngmo Dec 22 '20 at 14:53
  • @TedLyngmo I am new to this... could you give a link – jdl Dec 22 '20 at 14:57
  • 1
    The crontab man page: https://man7.org/linux/man-pages/man5/crontab.5.html, see "environment setting" in the first section. – Benjamin W. Dec 22 '20 at 14:58
  • Where you can set environmental variables in your `crontab` file depends on the version of cron. See https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use. Look at the `crontab(5)` manpage for your OS version. – fpmurphy Dec 22 '20 at 15:02
  • The way to see the man page from the shell is `man crontab`. In some cases you have to specify the "section" of the manual as `man 5 crontab`. That's what the 5 in `crontab(5)` is above. – Andy Lester Dec 22 '20 at 15:19
  • As the crontab(5) man page indicates, use LOGNAME instead. – glenn jackman Dec 22 '20 at 16:17
  • I could get the user across using USER=experiment. – jdl Dec 22 '20 at 17:11
  • However I need the full scope to be in "experiment". I tried ` */1 * * * * . $HOME/.profile; /home/experiment/username ` as suggest else where... but didn't work. – jdl Dec 22 '20 at 17:13

1 Answers1

0

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"
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47