# How to shutdown using cron ?



## bgroper (Dec 9, 2022)

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 ?


----------



## Argentum (Dec 9, 2022)

bgroper said:


> 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 :
> 
> ...


Try to create a small one-line *shell script* with that *shutdown *command and run it from cron.


----------



## cracauer@ (Dec 9, 2022)

Try to capture the output. You probably don't get the usual email with output because all the machinery for mail goes down.


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

Also try 
	
	



```
halt -p
```
 directly in case some thing in shutdown is skipped, which is what shutdown ends up calling.


----------



## Argentum (Dec 10, 2022)

bgroper said:


> 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:


```
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:

```
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:
```


----------



## bgroper (Dec 12, 2022)

Thanks for suggestions above.


Argentum said:


> Did an experiment on a VM and all seems to be good. Created a small script:
> 
> 
> ```
> ...


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

```
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 ?


----------



## cracauer@ (Dec 12, 2022)

bgroper said:


> 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.


----------



## bgroper (Dec 12, 2022)

cracauer@ said:


> Try to capture the output. You probably don't get the usual email with output because all the machinery for mail goes down.
> 
> 
> ```
> ...


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 :

```
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.


----------



## facedebouc (Dec 12, 2022)

Did you try with at(1) ?


----------



## cracauer@ (Dec 12, 2022)

That's crazy. Now I'm curious.

Does it work if you wrap it into a shellscript like this?

```
#! /bin/sh
/sbin/halt -p &
sleep 666
wait
```

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


----------



## bgroper (Dec 14, 2022)

facedebouc said:


> Did you try with at(1) ?


Yes, I did try using /usr/bin/at to run the custom power_off script.
It works exactly as intended.  

More testing to come as per cracauer@  above.


----------

