Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crontab PATH and USER

I am new to scheduling tasks with cron and crontab. I am trying to schedule execution of a task as if I had logged on, opened a terminal, and executed it myself.

However, I scheduled a task to help me observe what $USER and $PATH a scheduled task is executing with, and this is what I found:

$ crontab -l 41 11 * * * echo "USER: $USER" > ~/Desktop/cron_env.log; echo "PATH: $PATH" >> ~/Desktop/cron_env.log $ cat ~/Desktop/cron_env.log USER: PATH: /usr/bin:/bin 

It appears as though $USER is not set, and $PATH is something very basic and/or default. On the contrary, this is what I see when I open a terminal (logged in) and echo this same information:

USER: aschirma PATH: /usr/lib/jvm/java-6-sun/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/pkg/icetools/bin:/pkg/hwtools/bin:/pkg/netscape/bin:/pkg/gnu/bin 

What do I need to do to make my crontab tasks run the way I want?

like image 517
Adam S Avatar asked Apr 12 '12 18:04

Adam S


People also ask

How do I see all crontab for users?

You can find them in /var/spool/cron/crontabs. The tables contain the cron jobs for all users, except the root user. The root user can use the crontab for the whole system. In RedHat-based systems, this file is located at /etc/cron.

What is default path for crontab?

Crontab Variables The default path is set to PATH=/usr/bin:/bin . If the command you are executing is not present in the cron specified path, you can either use the absolute path to the command or change the cron $PATH variable.

How do I change the path of crontab?

Run crontab and grab $PATH value used by crontab. Now edit crontab again to set your desired java bin path: a) crontab -e ; b) PATH=<value of $JAVA_HOME>/bin:/usr/bin:/bin (its a sample path); c) now your scheduled job/script like */10 * * * * sh runMyJob.sh & ; d) remove echo $PATH from crontab as its not needed now.

How do I assign a user to crontab?

Or more simply, you could just run crontab -e when logged in as that user. Alternatively, you could prefix your command in your (root) crontab with sudo -u <username> to run the command as the specified user.


1 Answers

According to "man 5 crontab" you can set environment variables in your crontab, by writing them before your cron lines.

There is also an example of a crontab so you just have to copy/paste it :

$ man 5 crontab | grep -C5 PATH | tail  # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do.  SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  # m h dom mon dow usercommand 17 * * * *  root  cd / && run-parts --report /etc/cron.hourly 25 6 * * *  root  test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7  root  test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 

So you can adjust your PATH or any environment variable to whatever you want. But this example seems enough for typical cases.

like image 122
Julien Palard Avatar answered Oct 11 '22 15:10

Julien Palard