Question of the Week: How can I set environment variables for my cron jobs?
Emre ,
- Product
- Cronitor
- Sent from
- cronitor.io
- Sent
Emre, The question this week comes from a developer who had the unfortunate discovery that none of his environment variables are available when he runs a cron job, even when he ran it as himself. We've been there before friend, and we've got your back. Why your environment variables are missing When you open your terminal you start an interactive shell, in this example using bash, your .bashrc file is automatically invoked. When this script runs, any environment variables it exports are available to every command you run in your terminal session. This method of setting environment variables in your .bashrc is the most common way that persistent environment variables are set, but your cron jobs do not invoke an interactive shell, and your .bashrc is not automatically loaded. This is true even if you specify /bin/bash as your crontab shell. How to set environment variables for your jobs When setting environment variables for your cron jobs, you have several options depending on your specific requirements, including: 1. Declaring variables directly in your crontab files. 2. Declaring variables as part of your command line. 3. Loading variables from a file before executing your command. 1. Declare the variables directly in your crontab In this example, we are configuring a PATH variable and a custom application variable. When the cron job runs, it will look for example.sh in the PATH provided, and when the command is run, its environment will include the PATH and APP_ENV variables too. You can set as many different variables as you need for your jobs, and they can go anywhere in your crontab file. APP_ENV=production PATH=/root/it/scripts 0 0 * * * example.sh One advantage of setting these variables directly in your crontab like this is that they are clear and easy to find. Anybody trying to understand the crontab file down the road will see every variable required by its jobs. Unlike bash scripts, the "export" keyword is not needed and not allowed. 2. Declare the variables as part of your command When you run a command on Linux and other "posix" operating systems, environment variables can be declared inline, and this works with crontab too. Here you can see an example where two jobs are getting different values for the same variable. 0 * * * * APP_ENV=production /var/data-analytics/export-to-datalake.py # Every hour 0 0 * * * APP_ENV=staging /var/data-analytics/export-to-datalake.py # Once a day This is the best way to set environment variables if you need different values in different jobs. (In this example we are only setting one variable, but you can set as many as needed.) 3. Load variables from a file When you have a lot of environment variables, or if they are centrally controlled by something like Ansible or Consul, you may have them declared directly in a file that you want to re-use everywhere. In this case, you can source that file directly as part of your command string. Any variables exported in the file will be part of the environment for the job. 0 * * * * source /var/data-analytics/bootstrap.sh ; /var/data-analytics/export-to-datalake.py View on the Web [https://cronitor.io/guides/cron-environment-variables] Cronitor Dashboard [https://cronitor.io/app] | Welcome to Cronitor [https://cronitor.io/app/welcome] You are receiving this email because you opted in to receive updates from Cronitor Cronitor, 1214 Parker Street, Berkeley, CA 94702 Unsubscribe [https://app.loops.so/unsubscribe/cmwifinux076101ve045ndq3y/78b23ecb577d93660acfb657a5afff3842a921dc012577248cb4ff05c60cc4abbbfe73cab2dcbe3d378420fc9e5293a272c39adfd82a24a8f3d40b35]
- Accession
- nse/cronitor/2026-11-15/7034
- From
- Cronitor · support@cronitor.io
- Sent
- 15 November 2026 · 22:00 UTC














