How to set cron PATH globally (i.e. for all users) permanently?

Solution 1:

You can configure your PATH in crontab configuration file as shown in the first code except there. First specify the env variables, then specify jobs.

UPD: Due to fact that link is broken, here's an excerpt:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

Solution 2:

Setting the PATH variable should work in Ubuntu, how do you say it is not working ?

Refer #14: Linux Crontab: 15 Awesome Cron Job Examples

Solution 3:

I could not find a solution for this either. The closest I got to a decent solution is the following (taken from https://raymii.org/s/tutorials/Better_cron_env_and_shell_control_with_the_SHELL_variale.html).

  • Change the shell for your cron job and point it to a bash script. I.e., at the top of the cronjob, add: SHELL=/path/to/setup/cron.bash
  • In this shell script, load environment variables and specify other vars. Make sure to include the following 4 lines at the top. It resets the SHELL variable to bash, and executes a bash shell to run the cronjobs.

For example:

#!/bin/bash
set -e

source /etc/environment
source /etc/profile
# restore SHELL env var for cron
SHELL=/bin/bash

# execute the cron command in an actual shell
exec /bin/bash --norc "$@"

Downside: this requires you to specify the SHELL=... at the top of every cronjob. Upside: you'll be using regular environment variables, and you won't have to worry about keeping variables consistent between cron and others