Page 1 of 1

Raspbian crontab

Posted: Sun Jul 05, 2015 10:39 pm
by dearsgroup
I have been reading several posts regarding automated scripting using the famous crontab. I haven't been successful yet and need someone to kindly point me in the right direction. Attached are my two statics.
crontab_schedule.JPG
crontab scheduling
crontab_schedule.JPG (11.59 KiB) Viewed 2237 times
crontab.JPG
my update script
crontab.JPG (17.51 KiB) Viewed 2237 times

Re: Raspbian crontab

Posted: Mon Jul 06, 2015 4:34 am
by tpylkko
how have you verified that this does not work? are you aware that the pi has to be powered on and running at the time specified in cron? if this is not possible, you should look at anacron. is the script matked as executable? are you sure you want to run rpi-update automatically?

Re: Raspbian crontab

Posted: Mon Jul 06, 2015 9:16 am
by rpdom
tpylkko wrote:are you sure you want to run rpi-update automatically?
or even at all.

If you are having problems with getting a cron job to work, you should probably no run rpi-update at all as it may leave your system in a state where you need a bit of experience to fix it.

About your crontab entry

Code: Select all

# m h  dom mon dow   command
  6 15 5    *   *   sh /home/pi/scripts/update.sh
You've set that to run at 3:06pm on the 5th day of every month. Is that what you intended?

It should work though. You could always add some debugging options, like this

Code: Select all

# m h  dom mon dow   command
  6 15 5    *   *   sh /home/pi/scripts/update.sh > /home/pi/logs/update.log 2>&1
That will send any output from the script to the file /home/pi/logs/update.log so you can check for errors. (You'll need to create a logs directory if you haven't already got one.

Just one last question. Are you using "crontab -e" to edit the file, or "sudo rcontab -e"?

Re: Raspbian crontab

Posted: Tue Jul 07, 2015 3:49 pm
by dearsgroup
I know it's not running because I sat and watched as the time for execution came and went. I saw no activity on my headless pi at the command line. And, yes, I would like to run an update script once a month or so in order to keep the pi updated. I believe, based on what I've read about crontab on rpi.org as well as here, that my setup was correct. Did I miss some step?

Re: Raspbian crontab

Posted: Tue Jul 07, 2015 3:52 pm
by dearsgroup
tpylkko wrote:how have you verified that this does not work? are you aware that the pi has to be powered on and running at the time specified in cron? if this is not possible, you should look at anacron. is the script matked as executable? are you sure you want to run rpi-update automatically?
I would prefer not to have to manually update if possible and always updating on bootup, I believe, is not necessary. Crontab or anacron (not sure what this is yet and will check) is the solution.

Re: Raspbian crontab

Posted: Tue Jul 07, 2015 3:53 pm
by dearsgroup
rpdom wrote:
tpylkko wrote:are you sure you want to run rpi-update automatically?
or even at all.

If you are having problems with getting a cron job to work, you should probably no run rpi-update at all as it may leave your system in a state where you need a bit of experience to fix it.

About your crontab entry

Code: Select all

# m h  dom mon dow   command
  6 15 5    *   *   sh /home/pi/scripts/update.sh
You've set that to run at 3:06pm on the 5th day of every month. Is that what you intended?

It should work though. You could always add some debugging options, like this

Code: Select all

# m h  dom mon dow   command
  6 15 5    *   *   sh /home/pi/scripts/update.sh > /home/pi/logs/update.log 2>&1
That will send any output from the script to the file /home/pi/logs/update.log so you can check for errors. (You'll need to create a logs directory if you haven't already got one.

Just one last question. Are you using "crontab -e" to edit the file, or "sudo rcontab -e"?
I am using "crontab -e" for user pi. I don't believe I should sudo because of elevated status. Am I correct?

Re: Raspbian crontab

Posted: Tue Jul 07, 2015 4:00 pm
by RaTTuS
you are going to end up with a broken system ...

however
for doing elevated stuff use
sudo crontab -e
for the roots contrab
no need to have sudo in it
then have using the full paths
/usr/bin/apt-get update && /usr/bin/apt-get upgrade -y
and put in the time as previous ....

if you an rpi-update into the mix you will get hit by a non working system at some stage

you will not see anything on the console unless you are piping to the one you are watching

Re: Raspbian crontab

Posted: Tue Jul 07, 2015 5:04 pm
by dearsgroup
Thanks. Will do.

Re: Raspbian crontab

Posted: Tue Jul 07, 2015 5:21 pm
by Joe Schmoe
Isn't it a lot easier (and better advice to be giving newbies) to put the 'sudo' in the cron job instead of making it part of the 'crontab' command line (thereby editing some other crontab file - that the user may not be aware of, or where it is) ?

I.e. instead of "sudo crontab -e" - which, as we've seen causes all sorts of confusion - make your cron entry (under the normal, 'pi' user):

v w x y z <tab> sudo commandThatRequiresRootPrivs

Among other things, this more closely mirrors how people run things from the pi command line.

Re: Raspbian crontab

Posted: Wed Jul 08, 2015 8:09 am
by RaTTuS
Joe Schmoe wrote:Isn't it a lot easier (and better advice to be giving newbies) to put the 'sudo' in the cron job i....
not if the the sudo then requires a password [which I know in default situations it does not now] however that may change or the user may have changed it.
putting sudo as part of the command ends up the user doing sudo for everything and will lead to other problems.

personally I don't like sudo not to use a password.

and I'm all for putting the stuff that needs root in the right place to begin with ...

though it has to be said again
automated upgrades are not usually the best ;-p

Re: Raspbian crontab

Posted: Wed Jul 08, 2015 9:51 am
by Joe Schmoe
not if the the sudo then requires a password...
I agree with all of your (theoretical) points.

But as things stand now (and will for the forseeable future), it works as I've described, and it is simpler for the newb.
...though it has to be said again
automated upgrades are not usually the best...
Agreed, but then again, Windows these days is all like that.

Anyway, I was thinking more of the other common case - where people want to run some Python script that uses GPIO and therefore requires sudo, via crontab.

Re: Raspbian crontab

Posted: Wed Jul 08, 2015 7:16 pm
by tpylkko
Crontab leaves a line in the logs when it runs, so you might want to check that in situations when you are not sure if it is doing what you want.