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
Load Environment Variables in a Cron Job
When crontab runs a command, it doesn't read any environment variables from files like ~/.bashrc, ~/.bash_profile, etc. Because cron runs tasks from a non-interactive, non-login shell, it operates with a minimal environment. Some applications require specific environment variables to function correctly, making it necessary to load them explicitly in cron jobs.
This article explores different methods for loading environment variables in crontab to ensure your scheduled tasks have access to the required configuration.
Setting the BASH_ENV Variable
We can set environment variables for shell scripts by using the BASH_ENV variable. When BASH_ENV is set, bash executes the specified file before running the script, allowing us to load environment variables from configuration files.
Let's add a line to our crontab file that sources /etc/profile before executing the script
* * * * * BASH_ENV=/etc/profile /home/user/print_envs.sh
This job runs every minute. Let's create the /home/user/print_envs.sh script to print all environment variables to a file
#!/bin/bash printenv > /tmp/print_envs_result
After setting execution permission with chmod +x /home/user/print_envs.sh, we can check the results
$ wc -l /tmp/print_envs_result 38 /tmp/print_envs_result $ grep PS1= /tmp/print_envs_result PS1=\u@\h:\w\$
The script loads 38 environment variables, including the PS1 prompt variable from /etc/profile.
Custom Preload Script
You can create a custom shell script to load multiple files. Let's create preload.sh that sources several configuration files and exports additional variables
#!/bin/bash . /etc/profile . ~/.bash_profile . ~/.bashrc export LEARNING_FROM=tutorialspoint
Modify the crontab to use this preload script
* * * * * BASH_ENV=/home/user/preload.sh /home/user/print_envs.sh
The result shows more variables loaded
$ wc -l /tmp/print_envs_result 41 /tmp/print_envs_result $ grep LEARNING_FROM /tmp/print_envs_result LEARNING_FROM=tutorialspoint
Wrapping The Job With Bash
For commands that aren't shell scripts, we can wrap them with bash using the -c option. This allows us to use BASH_ENV even for simple commands like printenv.
* * * * * BASH_ENV=/etc/profile bash -c "printenv > /tmp/print_envs_result"
The bash -c prefix executes the command in a bash shell that respects the BASH_ENV variable.
Complete Wrapper Script
Alternatively, create a complete wrapper script /home/user/wrap_printenvs.sh
#!/bin/bash . /etc/profile . ~/.bash_profile . ~/.bashrc export LEARNING_FROM=tutorialspoint # Run the original job printenv > /tmp/print_envs_result
Then simplify the crontab entry
* * * * * /home/user/wrap_printenvs.sh
Running The Job With a Login Shell
Using bash -l -c runs the command in a login shell, which automatically reads startup files like ~/.bash_profile and /etc/profile
* * * * * bash -l -c "printenv > /tmp/print_envs_result"
Since login shells don't load ~/.bashrc by default, combine it with BASH_ENV for complete environment loading
* * * * * BASH_ENV=~/.bashrc bash -l -c "printenv > /tmp/print_envs_result"
Setting Variables Directly in Crontab
You can set individual environment variables directly before the command using VARIABLE=value syntax
* * * * * LEARNING_FROM=tutorialspoint LANG=en_US /home/user/print_envs.sh
Multiple variables are separated by spaces. After execution
$ grep -E 'LANG|LEARNING_FROM' /tmp/print_envs_result LEARNING_FROM=tutorialspoint LANG=en_US
System-wide Variables
In /etc/crontab, you can define environment variables globally for all system cron jobs
LEARNING_FROM=tutorialspoint LANG=en_US * * * * * root /home/user/print_envs.sh
These variables apply to all subsequent cron jobs in the file.
Comparison of Methods
| Method | Best For | Advantages | Disadvantages |
|---|---|---|---|
| BASH_ENV | Shell scripts | Flexible, loads full environment | Only works with bash scripts |
| Bash Wrapper | Any command | Works with any command | Slightly more verbose syntax |
| Login Shell | User-specific jobs | Automatic profile loading | May not load all configs |
| Direct Variables | Few specific variables | Simple and explicit | Unwieldy for many variables |
Conclusion
Loading environment variables in cron jobs is essential for applications that depend on specific configurations. The BASH_ENV method provides the most flexibility for shell scripts, while bash wrappers work for any command. Choose the method that best fits your specific requirements and complexity needs.
