Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export USER env variable for use in cron

Tags:

bash

cron

ubuntu

I have a script that requires the env variable USER to be set. As the script is used by several users, I can't just do export USER=xxx at the beginning of the script. I could define in the crontab, but I was just wondering whether there is a good way of pulling it in.

I tried sourcing .bashrc and .profile, but neither define USER, plus on Ubuntu .bashrc simply returns on non-interactive shells.

like image 847
Cookie Avatar asked Jan 15 '23 23:01

Cookie


1 Answers

You could work around it by writing at the top of the script (Bashism):

USER=$(whoami)

or old-style:

USER=`whoami`

... assuming you have whoami in the PATH, which can also be set in the crontab just like several (most?) other variables. I.e. you can also set the variable in crontab itself (at least in Vixies cron) - see here for example.

like image 70
0xC0000022L Avatar answered Jan 17 '23 11:01

0xC0000022L