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

10

1

We need to have /usr/local/bin in cron's path for all users. Is there a way to set it system-wide, without needing to edit each individual user's crontab?

We've tried adding PATH to /etc/crontab:

# grep PATH /etc/crontab
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

But when users have this in their crontab:

$ crontab -l | grep PATH
* * * * * echo $PATH > /tmp/current_cron_path

...it reveals that their path is still set to default:

$ cat /tmp/current_cron_path
/usr/bin:/bin

grosser

Posted 2010-07-16T12:30:14.373

Reputation: 355

2General hint: Writing "it didn't work" will not usually get you a useful reply. Always describe how exactly it didn't work, that is, exactly what did you do, and what exactly was the result. – sleske – 2010-07-19T10:14:14.833

Please note that relying on echo in your cron is possibly unreliable.

– Der Hochstapler – 2012-06-12T15:07:21.217

Answers

4

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

igorp1024

Posted 2010-07-16T12:30:14.373

Reputation: 431

The link is broken. – Bjorn – 2015-01-03T13:40:57.617

I've updated my answer. – igorp1024 – 2015-01-04T16:02:53.270

1does not work on ubuntu, even after restarting cron – grosser – 2010-07-19T07:01:45.447

1@grosser: Well, it should. Please post your complete crontab (edit your answer), along with the output it produces and why you believe it does not work. Then we'll see... – sleske – 2010-07-19T10:06:28.093

Also note that the format of the VARIABLE=value lines is somewhat more restricted than in a shell script: Each assignment must be on a line of its own, and you may not use variables on the right-hand-side (e.g. PATH=$PATH:/bla will not work). – sleske – 2010-07-19T10:12:27.490

i updated the question, setting the PATH in an individual crontab did work, just the global change did not work – grosser – 2010-07-21T06:10:03.793

Try specifying PATH variables in each crontab where you need it. Probably it's done due to security issues.

BTW, in ubuntu (I've checked) PATH is defined in /etc/crontab and /etc/cron.d/anacron in ubuntu. So it seems that it is not being inherited from /etc/crontab. Just try defining it in root's crontab (sudo crontab -e) – igorp1024 – 2010-07-21T14:53:18.263

3

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

thegeek

Posted 2010-07-16T12:30:14.373

Reputation: 2 980

i updated the question, setting the PATH in an individual crontab did work, just the global change did not work – grosser – 2010-07-21T06:22:32.657

1

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

Laurens Rietveld

Posted 2010-07-16T12:30:14.373

Reputation: 111

0

If you're able to be root, can you try editing /etc/init.d/cron and changing the PATH there? I haven't tested this, but would be interesting to check.

Rich Homolka

Posted 2010-07-16T12:30:14.373

Reputation: 27 121

did not seem to work, i just added PATH=xxx into /etc/init.d/cron but the * * * * * echo $PATH output was the same – grosser – 2010-08-05T07:21:25.130