When issues arise using cron to schedule events, a frequently-heard explanation is that cron runs with a different set of environment variables than a "normal" user (e.g. pi). That's all well and good, but what is the environment for the cron user? If one is to avoid errors due to an incorrect environment when using cron, it would be useful to know what that environment is.


We can ask cron to tell us what its environment is.

  • Create a shell script in your home directory (~/) as follows (or with the editor of your choice):
$ nano ~/envtst.sh
  • Enter/C+P the following in the editor:
echo "env report follows for user "$USER >> /home/pi/envtst.sh.out 
env >> /home/pi/envtst.sh.out 
echo "env report for user "$USER" concluded" >> /home/pi/envtst.sh.out
echo " " >> /home/pi/envtst.sh.out
  • Save the file and exit the editor; then set the file permissions as executable, and open your crontab for editing:
$ chmod a+rx ~/envtst.sh
$ crontab -e
  • Enter the following line at the bottom of your crontab:
* * * * *  /home/pi/envtst.sh >> /home/pi/envtst.sh.err 2>&1
  • Save and exit your crontab. Use tail to view the output & (hopefully) observe the environment for cron. If there's nothing in the file after a minute, view the file ~/envtst.sh.err for error messages, and adjust as required. (NOTE: If you want to clear all prior error messages after troubleshooting: $ > ~/envtst.sh.err )
crontab: installing new crontab
$ tail -f ~/envtst.sh.out
env report follows for user 
env report for user  concluded

This will repeat every minute, so enter ^C to stop the tail listing, edit your crontab again to "comment out" (or delete) the line just added. Save and exit the editor.

  • Note in the tail output above that cron has a rather sparse environment; only six (6) variables are used to define it. Note the PATH consists of only two directories. This is why your crontab entry fails if, for example, you're trying to launch a Python script that resides in your home directory. Note also that the user name (aka LOGNAME iaw System V) isn't cron - it's pi!

  • If you're not familiar, with your own user environment, it's useful to compare it against the cron environment. We'll use the same shell script to add that to the "output" file ~/envtst.sh.out:

$ ~/envtst.sh 
  • To view the output, open ~/envtst.sh.outin your editor, or cat ~/envtst.sh.out to see it in your terminal. It will likely be a fairly extensive output; 30 lines of text, more or less. Note in particular the following lines (assuming you've run this as user pi) :
  • You'll notice numerous differences in the two environments. This will help create rational cron jobs, and help troubleshooting when they don't behave as you'd like.
  • Very nice! For some reason your $USER variable isn't set -- see env report for user concluded. – Mark Smith Mar 26 at 7:22
  • @MarkSmith: If you're referring to the absence of $USER in the cron environment, it's not that it's not set... it's just not used in the version of cron on Raspbian (and Debian I think). Here's some more on that, and still more, and more. Likely more to this story, but I don't think $USER defined for cron in any Raspbian distro – Seamus Mar 26 at 14:17
  • @MarkSmith: It just dawned on me the line you referenced: env report for user concluded. Yeah... :) I stuck that in the script to emphasize that $USER isn't defined :P – Seamus Mar 26 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.