I have a crontab running every hour. The user running it has environment variabless in the .bash_profile
that work when the user runs the job from the terminal,
You can also prepend your command with env
to inject Environment variables like so:
0 * * * * env VARIABLE=VALUE /usr/bin/mycommand
sudo sh -c "echo MY_GLOBAL_ENV_TO_MY_CURRENT_DIR=$(pwd)" >> /etc/environment"
crontab -e
*/5 * * * * sh -c "$MY_GLOBAL_ENV_TO_MY_CURRENT_DIR/start.sh"
=)
For me I had to specify path in my NodeJS file.
// did not work!!!!!
require('dotenv').config()
instead
// DID WORK!!
require('dotenv').config({ path: '/full/custom/path/to/your/.env' })
Setting vars in /etc/environment
also worked for me in Ubuntu. As of 12.04, variables in /etc/environment
are loaded for cron.
If you start the scripts you are executing through cron with:
#!/bin/bash -l
They should pick up your ~/.bash_profile
environment variables
I'm using Oh-my-zsh
in my macbook so I've tried many things to get the crontab task runs but finally, my solution was prepending the .zshrc
before the command to run.
*/30 * * * * . $HOME/.zshrc; node /path/for/my_script.js
This task runs every 30 minutes and uses .zshrc
profile to execute my node command.
Don't forget to use the dot before the $HOME
var.