Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Where can I set environment variables that crontab will use?
Environment variables are typically set in shell configuration files like .bash_profile, .bashrc (Ubuntu), or .zshrc (macOS). However, crontab runs in a minimal environment and doesn't automatically load these shell configuration files, making environment variables unavailable to cron jobs.
Understanding the Problem
Let's examine a typical shell configuration file with environment variables −
immukul@192 dir1 % cat ~/.bash_profile export JAVA_HOME=$(/usr/libexec/java_home) export PATH=$PATH:/usr/local/node/bin export GOROOT=/usr/local/go export GOPATH=/Users/immukul/go_projects
These variables work perfectly in a normal terminal session −
echo $GOROOT
/usr/local/go
However, when you create a cron job, these variables are not available because cron operates with a minimal environment that doesn't source your shell configuration files.
Solutions for Making Environment Variables Available to Crontab
Method 1: Using Login Shell in Script
The most common approach is to use the -l (login) flag with bash in your cron script. First, create your cron job −
crontab -e
Add the following line to run your script every minute −
* * * * * /path/to/sample.sh
In your sample.sh script, use the login shell −
#!/bin/bash -l
echo "PATH: $PATH"
echo "GOROOT: $GOROOT"
for f in *.txt; do mv "$f" "${f// /_}"; done
The #!/bin/bash -l shebang tells the script to run as a login shell, which loads your .bash_profile and makes environment variables available.
PATH: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin GOROOT: /usr/local/go
Method 2: Setting Variables Directly in Crontab
You can define environment variables directly in your crontab file −
JAVA_HOME=/usr/lib/jvm/java-11-openjdk GOROOT=/usr/local/go PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin * * * * * /path/to/sample.sh
Method 3: Sourcing Configuration Files in Script
Explicitly source your shell configuration within the script −
#!/bin/bash source ~/.bash_profile echo "Using GOROOT: $GOROOT" # Your script commands here
Comparison of Methods
| Method | Pros | Cons |
|---|---|---|
| Login Shell (-l) | Loads all environment variables automatically | Slower startup, loads unnecessary configurations |
| Direct in Crontab | Fast, explicit control | Must duplicate variables, harder to maintain |
| Source in Script | Flexible, can choose which files to source | Requires manual sourcing in each script |
Conclusion
Cron jobs run in a minimal environment without access to your shell's environment variables. The most effective solution is using #!/bin/bash -l in your scripts to load the login shell environment. For production systems, consider setting variables directly in the crontab for better control and performance.
