Access $USER Environment Variable in a Cron Triggered Script in Ubuntu
Bash, Linux
When triggering a BASH script in a Cronjob in Ubuntu the script will not have access to the $USER
environment variable.
This is the case even if the script is trigerred by the user’s crontab. Fortunately this is easily fixed.
Within the crontab, $LOGNAME
represents the current user - so you can set the $USER
variable before calling your script:
…you can now use $USER
in the script as usual.
Background
Several environment variables are set automatically by cron:
SHELL
: is set to /bin/shLOGNAME
: is set from the line in/etc/passwd
corresponding to the crontab’s ownerHOME
: is set from the line in/etc/passwd
corresponding to the crontab’s owner
Note that HOME
and SHELL
can be overridden in the crontab but LOGNAME
cannot.
References
comments powered by Disqus