User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Cron job to shut down at a set time

Tue Oct 29, 2013 2:42 pm

I want to write a cron job to shut down the pi and later reboot it using a timer switch to power it up at a certain time.

I can manage the cron syntax but none of the shutdown/reboot commands work except for:

reboot -f

(Reboot fast, by suppressing the normal call to fsck when rebooting.)

Which reboots , but I need shutdown.

There must be a problem somewhere which stops the pi shutting down.

I have updated the pi.

Can anyone assist?

Examples tried:
sudo shutdown -f
sudo shutdown -h now (also '0')
sudo shutdown -n
sudo reboot
sudo reboot -n
sudo reboot -w
sudo reboot -d

User avatar
RaTTuS
Posts: 10828
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 2:56 pm

sudo crontab -e
[add a root user crontab]
30 15 * * * /sbin/shutdown -h now
[30mins past 3PM shutdown ]
be aware that if you don't have a internet connection then your times are going to drift
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 6:33 pm

Thanks for taking the time to respond, however, to test that it would work as I had previously tried shutdown -h I tried:

/sbin/shutdown -h now
which returned a segmentation error.

and

sudo /sbin/shutdown -h now
which returned to the x@raspberrypi ~ $ prompt

shutdown -h was one of the commands I had tried previously with both now and 0

Am I correct that the above is a valid test?

User avatar
DougieLawson
Posts: 42328
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 6:45 pm

How about
  • Using the. `at` command
    sudo apt-get install at # it's not installed automatically
    at 03:30 [enter]
    at> /sbin/dosomethingfunky
    at> ctrl+D
    Job 1 at Wed Oct 30 03:30:00 2013
  • Or use the timing function of the shutdown command
    sudo shutdown -h 03:30
Last edited by DougieLawson on Tue Oct 29, 2013 6:52 pm, edited 1 time in total.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 6:50 pm

The syntax is not the problem :(

The problem is that non of the relevant commands to shutdown appear to work.

User avatar
DougieLawson
Posts: 42328
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 6:54 pm

makem2 wrote:The syntax is not the problem :(

The problem is that non of the relevant commands to shutdown appear to work.
Stop using cron. Or if you must use cron, you'll need to use `sudo crontab -e` to add it to root's crontab.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 6:59 pm

Installed the 'at' for possible use and experimenting.

Got this error after install:

ALERT: exim paniclog /var/log/exim4/paniclog has non-zero size, mail system possibly broken

I don't suppose that has any bearing on the shutdown problem? I have never had any other errors.

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 7:08 pm

DougieLawson wrote:
makem2 wrote:The syntax is not the problem :(

The problem is that non of the relevant commands to shutdown appear to work.
Stop using cron. Or if you must use cron, you'll need to use `sudo crontab -e` to add it to root's crontab.
I know how to use cron and have 4 cron jobs running currently to backup from one HD to another.

I could make a cron job as posted previously but just wanted to test it world work outside cron. It did not because as I have said, no shutdown commands appear to work.

User avatar
Douglas6
Posts: 5159
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, USA

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 7:27 pm

Be aware that any shutdown command will not power down the Pi. It will shutdown the OS, but the Pi will stay on (drawing a minimal amount of current), and the red LED will stay on. The only way to turn off the Pi is to physically (or electrically) remove power.
Your timer switch would need to turn off and then on the power to reboot. Can you set it to cycle off some minutes after the cron job, and back on at the appointed time?

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 7:44 pm

In the past, when using the shutdown command, as you say, the red LED remained on and I assumed that was just a 'connected power' indication. I then was able to remove the SD card without risking corruption.

However, now, when I use that command, all the LEDs stay on and I am still connected and can operate the pi headlessly.

That is the problem.

User avatar
Douglas6
Posts: 5159
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, USA

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 8:14 pm

Ok, sorry, just making sure. That is certainly odd. First I've heard of this problem, so I can't help. But those who can will probably need some more details. I don't think you mentioned which OS, for example. And any output from the non-working command, standard error log entries if any, relevant changes between the time it was working and now, etc.

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 8:49 pm

The OS is Raspbian
There are numerous logs in /var/logs most of which .gz and I have no idea which log would have relevant data in it. I am not sure how to examine the logs at this moment but will do some research into that.

Since the initial install I have only updated the OS and packages. I don't run any software except for that necessary to achieve an always on NAS with 2 HD's. The whole setup is very simple and basic.

Output from the shutdown command is either 'segmentation' if I do not use sudo in the command, or a return to the pi prompt if I do.

In the past I would 'lose' one of the HDs (HD1) and needed to reboot to get it back. This still happens now and again but it now happens with the other HD. (HD2) That is the only problem I have had. At that time I use the sudo reboot -f command to reboot and it works. However, now I have the shutdown problem I am unable remove the SD card unless I turn off the power which is not a good idea.

User avatar
joan
Posts: 16126
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 8:56 pm

If you are using cron to shut down please post the contents of your crontable.

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 9:22 pm

joan wrote:If you are using cron to shut down please post the contents of your crontable.
Currently:

0 6 * * * rsync -avx --delete /media/HDD1/shares/myprofile /media/HDD2/shares/
0 5 * * * rsync -avx --delete /media/HDD1/shares/hanprofile /media/HDD2/shares/
0 4 * * 7 rsync -avx --delete /media/HDD1/shares/mailwasher /media/HDD2/shares/
0 3 * * * rsync -avx --delete /media/HDD1/shares/archive /media/HDD2/shares/

I was going to add:

30 15 * * * /sbin/shutdown -h now (As suggested by an earlier poster - changing time as appropriate)

However, as I had tried sudo shutdown -h now at the pi prompt, I saw no point in including it in a cron until it worked outside a cron.

User avatar
joan
Posts: 16126
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 9:56 pm

Permissions?

shutdown requires root permission. It only works in the root crontab.

sudo crontab -e


will allow you to add that entry in the root crontab.

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:04 pm

Yes, I was aware of that.

But should sudo shutdown -h now not work as root?

I will put in the cron but the existing jobs all work fine outside the cron and I thought sudo shutdown -h now would also.

User avatar
joan
Posts: 16126
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:09 pm

makem2 wrote:Yes, I was aware of that.

But should sudo shutdown -h now not work as root?

I will put in the cron but the existing jobs all work fine outside the cron and I thought sudo shutdown -h now would also.
Perhaps sudo has timed out and requires a password?

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:19 pm

joan wrote:
makem2 wrote:Yes, I was aware of that.

But should sudo shutdown -h now not work as root?

I will put in the cron but the existing jobs all work fine outside the cron and I thought sudo shutdown -h now would also.
Perhaps sudo has timed out and requires a password?
Timed out? I have never heard that could happen.

sudo is working fine for other purposes.

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:23 pm

Ok, I have added:

30 22 * * * /sbin/shutdown -h now

to my crontab so at 22:30 the pi should shut down (fingers crossed?)

User avatar
joan
Posts: 16126
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:31 pm

makem2 wrote:Ok, I have added:

30 22 * * * /sbin/shutdown -h now

to my crontab so at 22:30 the pi should shut down (fingers crossed?)
Provided you have added that entry to the root crontab.

man sudo # for password timeout

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:34 pm

Well, it is now 22:33 and I still have full headless access to the pi.

So, shutdown does not work from a cron either, as I suspected it would not.

Can anyone tell me in which log to look to see if an error or some reference was made to the shutdown attempt?

User avatar
joan
Posts: 16126
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:38 pm

Post the contents of your root crontab

sudo crontab -l

bblood
Posts: 42
Joined: Sat Sep 22, 2012 1:54 am

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:39 pm

I use sudo halt..... Or /sbin/halt in cron to shut thins down. I have a solar powered pi and a relay on a timer to save power. Cron halts the pi about 10 minutes prior to the relay tripping power to it.....then at about 6 am the relay tripps again and the pi reboots.....been working like a champ for a long time.....almost a year...

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:40 pm

joan wrote:
makem2 wrote:Ok, I have added:

30 22 * * * /sbin/shutdown -h now

to my crontab so at 22:30 the pi should shut down (fingers crossed?)
Provided you have added that entry to the root crontab.

man sudo # for password timeout
Here is the current crontab:

0 6 * * * rsync -avx --delete /media/HDD1/shares/myprofile /media/HDD2/shares/
0 5 * * * rsync -avx --delete /media/HDD1/shares/hanprofile /media/HDD2/shares/
0 4 * * 7 rsync -avx --delete /media/HDD1/shares/mailwasher /media/HDD2/shares/
0 3 * * * rsync -avx --delete /media/HDD1/shares/archive /media/HDD2/shares/

30 22 * * * /sbin/shutdown -h now

User avatar
makem2
Posts: 233
Joined: Tue Jul 02, 2013 9:12 am
Location: Essex

Re: Cron job to shut down at a set time

Tue Oct 29, 2013 10:42 pm

If sudo has a timeout, at what time does the clock start ticking?

Sorry if I am being silly but it seems strange for a command to timeout.

Return to “Raspberry Pi OS”