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
Re: Cron job to shut down at a set time
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
[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
WARNING - some parts of this post may be erroneous YMMV
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe
Re: Cron job to shut down at a set time
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?
/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?
- 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
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.
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.
Re: Cron job to shut down at a set time
The syntax is not the problem
The problem is that non of the relevant commands to shutdown appear to work.

The problem is that non of the relevant commands to shutdown appear to work.
- 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
Stop using cron. Or if you must use cron, you'll need to use `sudo crontab -e` to add it to root's crontab.makem2 wrote:The syntax is not the problem![]()
The problem is that non of the relevant commands to shutdown appear to work.
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.
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.
Re: Cron job to shut down at a set time
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.
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.
Re: Cron job to shut down at a set time
I know how to use cron and have 4 cron jobs running currently to backup from one HD to another.DougieLawson wrote:Stop using cron. Or if you must use cron, you'll need to use `sudo crontab -e` to add it to root's crontab.makem2 wrote:The syntax is not the problem![]()
The problem is that non of the relevant commands to shutdown appear to work.
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.
Re: Cron job to shut down at a set time
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?
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?
Re: Cron job to shut down at a set time
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.
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.
Re: Cron job to shut down at a set time
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.
Re: Cron job to shut down at a set time
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.
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.
Re: Cron job to shut down at a set time
If you are using cron to shut down please post the contents of your crontable.
Re: Cron job to shut down at a set time
Currently:joan wrote:If you are using cron to shut down please post the contents of your crontable.
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.
Re: Cron job to shut down at a set time
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.
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.
Re: Cron job to shut down at a set time
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.
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.
Re: Cron job to shut down at a set time
Perhaps sudo has timed out and requires a password?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.
Re: Cron job to shut down at a set time
Timed out? I have never heard that could happen.joan wrote:Perhaps sudo has timed out and requires a password?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.
sudo is working fine for other purposes.
Re: Cron job to shut down at a set time
Ok, I have added:
30 22 * * * /sbin/shutdown -h now
to my crontab so at 22:30 the pi should shut down (fingers crossed?)
30 22 * * * /sbin/shutdown -h now
to my crontab so at 22:30 the pi should shut down (fingers crossed?)
Re: Cron job to shut down at a set time
Provided you have added that entry to the root crontab.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?)
man sudo # for password timeout
Re: Cron job to shut down at a set time
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?
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?
Re: Cron job to shut down at a set time
Post the contents of your root crontab
sudo crontab -l
sudo crontab -l
Re: Cron job to shut down at a set time
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...
Re: Cron job to shut down at a set time
Here is the current crontab:joan wrote:Provided you have added that entry to the root crontab.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?)
man sudo # for password timeout
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
Re: Cron job to shut down at a set time
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.
Sorry if I am being silly but it seems strange for a command to timeout.