1

I have a cron job written to root (I'm writing to a log in /var/log so need root permission). The problem is that sometimes it works, and sometimes it doesn't.

The cron job is simply to call a python script in my home directory, this then does all the hard work.

I set the job to go off in the middle of the night so it doesn't interfere with what I'm doing and so that the API it interacts with is less likely to change (the API is updated throughout the day).

But every morning I check the logs and get the following error:

python3: can't open file '/home/myUser/path/to/the/main.py': 
[Errno 2] No such file or directory

My response to this is to change the timing on the crontab to a few minutes beyond the time I am checking the logs, wait a few minutes, and the file starts running. The only thing I change is the timing, and suddenly it works.

So I know, for sure, that the file exists.

I've tried a variation of times at night (30 23 * * *, 01 01 * * *, 10 02 * * *) none have worked, but whenever I set it off for 2 minutes after I check in the morning, it is fine.

Here is the crontab as it currently stands:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

01 01 * * * python3 /home/myUser/path/to/the/main.py >> /var/log/thisjobs.log 2>&1

It's also worth mentioning that there is an additional newline after this job (I've heard that it is necessary).

Has anyone seen this before? Is there anything I can try to get this file found without me having to manually change the script each time?

EDIT: here is my env output for cron:

LANGUAGE=en_GB:en
HOME=/root
LOGNAME=root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
LANG=en_GB.UTF-8
SHELL=/bin/sh
PWD=/root

Also thought I'd mention that I have tried using the following command (after the timing) to no avail (same error, but only with the main.py instead of the full path):

cd /home/myUser/path/to/the; python3 main.py
  • 1
    Obviously the system is up and running at that time right? – kos Apr 24 '15 at 19:14
  • It's a 24/7 server I maintain at home so yep, it's up and running. Checking the syslog it shows that the job is run at the correct time, and obviously the logs are made. – Scironic Apr 24 '15 at 19:16
  • 1
    Found this and this. Try to set the full path to your script in you cron instead of using ~/ or $HOME – kos Apr 24 '15 at 19:21
  • Thanks, already using full and absolute paths though. Currently looking at this: askubuntu.com/questions/309674/cron-fails-at-night?rq=1 which may be a possibility. – Scironic Apr 24 '15 at 19:24
1

Turns out this was the answer: Cron fails at night

I'm running this on my server (which I only ssh into) over night. So having an encrypted home drive meant that when I'm not logged in nothing is accessible to anything outside of home, including cron.

I moved my script to /usr/local/bin (there may be a better place to put it, but I didn't feel anything else was suitable) which incidentally meant that I can call it without any path at all (assuming you've set the PATH variable).

Now everything runs as it should.

| improve this answer | |

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