How to shutdown using cron ?

Hi forum
I'm using a reliable old Gigabyte mobo H77M-D3H with i5 CPU and FreeBSD 13.1-RELEASE-p3.
As root from the console, the following command correctly and immediately powers off the system :
/sbin/shutdown -p now
When I include this exact same command as a cron job in /etc/crontab (and run as root)
the system reboots at the cron nominated time. It does not power off.
I also tried using /sbin/poweroff in /etc/crontab but experienced exact same issue.
TIA's for any tips or clues for how to get this working as intended ?
 
Hi forum
I'm using a reliable old Gigabyte mobo H77M-D3H with i5 CPU and FreeBSD 13.1-RELEASE-p3.
As root from the console, the following command correctly and immediately powers off the system :

When I include this exact same command as a cron job in /etc/crontab (and run as root)
the system reboots at the cron nominated time. It does not power off.
I also tried using /sbin/poweroff in /etc/crontab but experienced exact same issue.
TIA's for any tips or clues for how to get this working as intended ?
Try to create a small one-line shell script with that shutdown command and run it from cron.
 
Try to capture the output. You probably don't get the usual email with output because all the machinery for mail goes down.

Code:
/sbin/shutdown -p now > /root/log 2>&1

Also try
Code:
halt -p
directly in case some thing in shutdown is skipped, which is what shutdown ends up calling.
 
When I include this exact same command as a cron job in /etc/crontab (and run as root)
the system reboots at the cron nominated time. It does not power off.
I also tried using /sbin/poweroff in /etc/crontab but experienced exact same issue.
TIA's for any tips or clues for how to get this working as intended ?
Did an experiment on a VM and all seems to be good. Created a small script:

Code:
root@Xenon ~# ls -l /root/script/power_off
-rwxr--r--  1 root  wheel  37 Dec 10 13:50 /root/script/power_off*
root@Xenon ~# cat /root/script/power_off
#!/bin/sh

shutdown -p now | logger

After that tried to run manually and from crontab. It is all good.

There is an entry in the syslog:
Code:
Dec 10 13:57:05 Xenon newsyslog[760]: logfile turned over due to -F request
Dec 10 14:00:00 Xenon root[793]: Shutdown NOW!
Dec 10 14:00:00 Xenon shutdown[790]: power-down by root:
 
Thanks for suggestions above.
Did an experiment on a VM and all seems to be good. Created a small script:

Code:
root@Xenon ~# ls -l /root/script/power_off
-rwxr--r--  1 root  wheel  37 Dec 10 13:50 /root/script/power_off*
root@Xenon ~# cat /root/script/power_off
#!/bin/sh

shutdown -p now | logger

After that tried to run manually and from crontab. It is all good.

There is an entry in the syslog:
Code:
Dec 10 13:57:05 Xenon newsyslog[760]: logfile turned over due to -F request
Dec 10 14:00:00 Xenon root[793]: Shutdown NOW!
Dec 10 14:00:00 Xenon shutdown[790]: power-down by root:
Thanks for your suggestion.
I created a power_off script with shutdown -p | logger command as per yours above.
Executed the power_off command from the cli, and confirmed it works as intended.
Then I changed the entry in /etc/crontab to execute the power_off command at 11.35.
Sadly, the system rebooted rather than powering off.
Here's the relevant lines from /var/log/messages
Code:
Dec 12 11:35:00 freebsd root[1419]: Shutdown NOW!
Dec 12 11:35:00 freebsd shutdown[1420]: power-down by root:
Dec 12 11:35:00 freebsd root[1419]: Shutdown NOW!
Dec 12 11:35:00 freebsd root[1419]: ^M
Dec 12 11:35:00 freebsd root[1419]: System shutdown time has arrived^G^G^M
Dec 12 11:35:00 freebsd console-kit-daemon[1167]: WARNING: Error waiting for native console 9 activation: Inappropriate ioctl for device
Dec 12 11:35:00 freebsd kernel: .
Dec 12 11:35:01 freebsd ntpd[1074]: ntpd exiting on signal 15 (Terminated)
Dec 12 11:35:01 freebsd kernel: , 1134.
Dec 12 11:35:01 freebsd kernel: .
Dec 12 11:35:02 freebsd syslogd: last message repeated 1 times
Dec 12 11:35:02 freebsd syslogd: exiting on signal 15
Dec 12 11:35:54 freebsd syslogd: kernel boot file is /boot/kernel/kernel
Dec 12 11:35:54 freebsd kernel: ---<<BOOT>>---
Dec 12 11:35:54 freebsd kernel: Copyright (c) 1992-2021 The FreeBSD Project.
Dec 12 11:35:54 freebsd kernel: Copyright (c) 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
Dec 12 11:35:54 freebsd kernel:         The Regents of the University of California. All rights reserved.
Dec 12 11:35:54 freebsd kernel: FreeBSD is a registered trademark of The FreeBSD Foundation.
Dec 12 11:35:54 freebsd kernel: FreeBSD 13.1-RELEASE-p3 GENERIC amd64

Is cron doing something unusual to keep the system alive ?
 
Is cron doing something unusual to keep the system alive ?

Cron provides stderr and stdout in a pipe and the program run might get a SIGPIPE when printing something but cron goes down. So shutdown executes the first steps of shutdown, which kill cron, prints something and before it gets to call /sbin/halt it had died with a SIGPIPE. Just a theory.

That is why I asked you to redirect stderr and stdout in my post above.

In any case, I don't see why you couldn't just call /sbin/halt -p directly instead of shutdown.
 
Try to capture the output. You probably don't get the usual email with output because all the machinery for mail goes down.

Code:
/sbin/shutdown -p now > /root/log 2>&1

Also try
Code:
halt -p
directly in case some thing in shutdown is skipped, which is what shutdown ends up calling.
Thanks for suggestions.
Unfortunately, /sbin/halt -p works as expecxted from the cli, but also insists on rebooting when executed by cron.
I tried logging some output using /sbin/halt -p > /root/log 2>&1 in /etc/crontab as suggested.
/root/log shows the following :
Code:
shutdown: [pid 1379]
Shutdown NOW!
Shutdown NOW!

System shutdown time has arrived
The system immediately reboots, so I'm still searching for a solution for this problem.
Meanwhile, very many thanks for all tips and clues.
 
That's crazy. Now I'm curious.

Does it work if you wrap it into a shellscript like this?
Code:
#! /bin/sh
/sbin/halt -p &
sleep 666
wait

Next we'll try wrapping it into a tmux session to give it a tty...
 
Back
Top