12

I had a problem running a script from crontab. After some research I understood the problem was because PATH parameter doesn't include /sbin.

I looked what it does include in /etc/crontab:

PATH=/sbin:/bin:/usr/sbin:/usr/bin

As a test - simple cron job to print the PATH variable:

* * * * * echo $PATH &> /root/TMP.log

the output is:

cat /root/TMP.log
/usr/bin:/bin

I don't understand this behaviour... How do I set the PATH variable..? Or better - how to add paths to it?

16

While they are similar, a user crontab (edited using crontab -e) is different from and keeps a separate path from the system crontab (edited by editing /etc/crontab).

The system crontab has 7 fields, inserting a username before the command. The user crontab, on the other hand, has only 6 fields, going directly into the command immediately after the time fields.

Likewise, the PATH in the system crontab normally includes the /sbin directories, whereas the PATH in the user crontab does not. If you want to set PATH for the user crontab, you need to define the PATH variable in the user crontab.


A simple workaround for adding your regular PATH in shell commands in cron is to have the cronjob source your profile by running bash in a login shell. for example instead of

* * * * * some command

You can instead run

* * * * * bash -lc some command

That way if your profile sets the PATH or other environment variables to something special, it also gets included in your command.

  • Thanks, that explains it... But this way I can only set the PATH, and not edit, because PATH=/sbin:$PATH or similar doesn't work. I tried... – csny Aug 3 '14 at 12:17
  • Yes, you would need to define the PATH from scratch in a case like this, which should be easy enough, since you know all of the commands that are run in your crontab, so you just need to take those into consideration. – beans Aug 3 '14 at 12:47
  • @madumlao bash -lc doesn't help. I'm good for now with setting PATH manually, but if I install some scripts, I want them to be recognized in cronjobs without specifying full paths. I'm sure it is possible somehow, and bash -lc is on the way to a solution :) – csny Aug 3 '14 at 15:42
  • 4
    bash -lc starts a login shell which sources your user's profile. This means that it uses the PATH set in ~/.bash_profile, ~/.profile, or ~/.bashrc... do you have the path set there? If so and it works ill update my answer for it – madumlao Aug 4 '14 at 3:34
  • Great. Thanks Madunlao. After I manually added bin file folder to ~/.bash_profile, my cronjob works well. – 张健健 Jun 30 '16 at 6:56
7

If you need default system wide PATHs and other ENV variables (which defined in /etc/profile.d), just put the following:

* * * * * . /etc/profile; your cmd
2

It is possible to set the PATH (or other parameters) in the cronjob line or on a single line. But you are not able to use shell expansion like PATH=$PATH:/usr/local/bin. But the tilde is allowed for home directory shortcuts.

From the books:

An active line in a crontab is either an environment setting or a cron command. An environment setting is of the form: name = value where the white spaces around the equal-sign (=) are optional, and any subsequent non-leading white spaces in value is a part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing white spaces.

See man 5 crontab for more information.

So this should work:

PATH=/bin:/usr/bin:/my/path/bin
5 3 * * * command_that_requires_my_path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.