jbobfunky
Posts: 23
Joined: Sat Oct 05, 2019 12:19 pm

Proper Crontab Syntax ?

Fri Jan 17, 2020 9:00 pm

Hello everyone,

I seem to be stumped on this one. Simply trying to run a python script with the linux crontab feature. I know the script works and that the cron is working using this code that outputs to a file every minute:

* * * * * /bin/echo "cron works" >> /tmp/file

However I can not seem to get this to work, which is supposed to fire at 12:30 AM everyday:

29 0 * * * /usr/bin/python3 /home/pi/Projects/Python/Backup_Script.py

No matter what time I choose however it still doesn't want to fire the command. I've look at many solutions but none seem to work and this appears to be so simple according to what I'm finding, but alas..I'm unable to get it to work.

If anyone has any knowledge about this, it would be greatly appreciated!! Thank you!

- jbobfunky
~ jbobfunky

User avatar
neilgl
Posts: 3349
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Proper Crontab Syntax ?

Fri Jan 17, 2020 9:28 pm

Are you using the user pi crontab

Code: Select all

crontab -e
or inadvertently using root crontab

Code: Select all

sudo crontab -e

pcmanbob
Posts: 11358
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Proper Crontab Syntax ?

Fri Jan 17, 2020 9:40 pm

So 12.30 midnight would be " 30 0 * * * "

And 12.30 midday would be " 30 12 * * * "

Look at this web site it's helpful when generating Cron lines

https://crontab-generator.org/

So your Cron line looks ok , so what does your called program do ?

Remember any files accessed with in your program must be called using the full directory path .
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

GlowInTheDark
Posts: 1821
Joined: Sat Nov 09, 2019 12:14 pm

Re: Proper Crontab Syntax ?

Fri Jan 17, 2020 9:50 pm

There are two critical things necessary in order to debug cron jobs:

1) Look in /var/log/syslog. There will be an entry there for each firing of a cron job. Most likely, and I mean this with all peace and love, you will find that the job *DID* run (since you cron line looks fine), but that it failed in some way.

2) Setup logging so that you can see what failed. The most straightforward way to do this (not the best way, but the most straighforward) is to append ">> /tmp/mylogfile.txt 2>&1" to the end of your cron line.

So, your line will be:

Code: Select all

30 0 * * * /usr/bin/python3 /home/pi/Projects/Python/Backup_Script.py >> /tmp/mylogfile.txt 2>&1
Now, when the job runs, you can check the logfile and see what happened.

And, you might as well run it during the day, like, say, a few minutes from now, rather than wait for it to run overnight.

There are various other comments I could make about use of cron in general, but I will hold them for now.
Poster of inconvenient truths.

Linux zealot and proud of it.

jbobfunky
Posts: 23
Joined: Sat Oct 05, 2019 12:19 pm

Re: Proper Crontab Syntax ?

Fri Jan 17, 2020 9:57 pm

I I think figured it out! I'm using just the crontab -e, but it's good to know there's a sudo as well. Thanks for that!

But yes, it was a path issue. One in the crontab command which I corrected and made the initial post when that hadn't worked. I realized right after the directory the script was outputting to wasn't the one I was looking in. So I had to actually change the script in the python code.

It's a simple piece that backups any directory I choose, or the whole sd card I believe and then moves it to a flash drive for archive. It's pretty sweet for a newbie who wants to keep things in tact as I learn this stuff. ;) It's been cool.

I appreciate the help! Forward!

-jbobfunky
~ jbobfunky

jbobfunky
Posts: 23
Joined: Sat Oct 05, 2019 12:19 pm

Re: Proper Crontab Syntax ?

Fri Jan 17, 2020 9:58 pm

The debug run down is sweet. I'll defo remember that. Thanks for that!

-jbobfunky
~ jbobfunky

Return to “General discussion”