Cronjob does not execute when not logged in

cronsyslog

SOLVED, see 'useless' answer.

I have a cronjob in my crontab.

When I specify the job to be executed in 2minutes, I wait, and then I can see files created by the job, I can see the process with ps. The job takes at least 10minutes to complete.

But, if I specify the job to be executed in 2minutes, and then I log out, I come back a few minutes thereafter, and it seems that the job has not been executed (no files created, and process not in ps).

When I look to the /var/log/syslog I can see that the job 'started', but immediately (at the same second) cron tries to send a mail. Probably the mail with the result. (well, sendmail is not correctly configured but this should not be the root cause).

Do you have any idea ?


Here are some concrete example : (python is the name of my machine)

I edit the crontab using crontab -e.
Here is the crontab :

user@python:~$ crontab -l
[...]
# m h  dom mon dow   command

50 9 * * * /home/user/scripts_automated/crontab1.sh

It should run at 9:50

Here is the content of the /var/log/syslog :

[...]
Nov  6 09:48:02 python crontab[30913]: (user) BEGIN EDIT (user)
Nov  6 09:48:18 python crontab[30913]: (user) REPLACE (user)
Nov  6 09:48:18 python crontab[30913]: (user) END EDIT (user)
Nov  6 09:50:01 python CRON[30936]: (user) CMD (/home/user/scripts_automated/crontab1.sh)
Nov  6 09:50:01 python sendmail[30938]: sA68o1a4030938: from=user, size=347, class=0, nrcpts=1, msgid=<201411060850.sA68o1a4030938@python>, relay=user@localhost
[...]

As you can see in the log, I edited the crontab at 09:48:02, then I logged out. I come back on the machine (with ssh), and the job did not produce any files it should have produced.

Here is the content of the script :

user@python:~$ cat scripts_automated/crontab1.sh


#  0  ------------------------------------------------------

datenow=`date +%F_%H-%M-%S`
/home/user/scripts_automated/script_1_cisco_grab.sh > /home/user/scripts_automated/crontab/run_${datenow}_.txt 2>&1 &
echo $! > /home/user/scripts_automated/crontab/pid_${datenow}.txt
ln -s /home/user/scripts_automated/crontab/pid_${datenow}.txt /home/user/scripts_automated/crontab/pid_last_run.txt

and no file is produced. I would at least expect the file pid_${datenow}.txt to exist with the pid of the background command. But nothing.
If I don't log out, then scripts executes, produces output files, pid_${datenow} file, and so on …

Best Answer

You should put your script in a separate directory, that for sure is available while the cron job is running, e.g. /usr/local/bin/.

It is also good custom to write crontab entries like this:

M H * * *  test -x /usr/local/bin/myscript.sh || /usr/local/bin/myscript.sh

so the crontab doesn't even try to execute the script if it is not available.


You suspected the logout being the problem, what you should have tried as well is logging out and then back in in-time for the crontab to run and then observe. That way you could have narrowed down if the logging out was the problem or the being not logged in.

Related Question