Skip to content

Instantly share code, notes, and snippets.

@four43
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save four43/4decc2dfb6476264f324 to your computer and use it in GitHub Desktop.
Save four43/4decc2dfb6476264f324 to your computer and use it in GitHub Desktop.
cron.d script that switches users in order to provide the called script with env vars
#Debian/Ubuntu
0 2 * * * root su --command="./my-script.sh" other-user -l
#RHEL/Fedora/Amazon AMI
0 2 * * * root su --session-command="./my-script.sh" other-user -l
#Install Script
echo " 0 2 * * * root su --session-command=\"./my-script.sh\" other-user -l" | sudo tee /etc/cron.d/my-crond-script
chmod 0644 /etc/cron.d/my-crond-script
@four43
Copy link
Author

four43 commented May 27, 2015

NOTE: Environment variables for the user must go in ~/.profile, not ~/.bashrc. bashrc is only loaded when a new terminal session is created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment