Automatically shutdown at night, automatically boot up in the morning

its not just the device itself. there are 2x 3,5 hhds and 2x 2,5 hdds attached (media center).

Also there is a (probably invalid) security scare from my spouse, that a “computer” runs the whole night, when it does not need to.

UPDATE 1:

i also checked now and unfortunately the script did not work.

**The Thin Client did NOT shut down at 00:05. I checked the next morning at 08:00 and it was still running. so i manually shut it down. **
the bash script said it should boot up 600 min (10 hrs) after shutdown and when i checked back at 11:00 the thin client was running again.

therefore i think something is wrong with the shutdown command. im trying to investigate
that is how the bash.sh script looks like:

nano shutwake.sh
#!/bin/bash
sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sh -c "echo `date '+%s' -d '+ 600 minutes'` > /sys/class/rtc/rtc0/wakealarm"
shutdown -h now

it seems that the code line does not work?:

shutdown -h now

the crontab looks as following

root@DietPi:~# cd /usr/local/bin
root@DietPi:/usr/local/bin# nano shutwake.sh
root@DietPi:/usr/local/bin# crontab -e -u root
05 00 * * * /usr/local/bin/shutwake.sh

UPDATE 2:

if i execute this command from the bash script in terminal, then thin clients immediately shuts down.

shutdown -h now

Maybe you can try to use the full path

/usr/sbin/shutdown -h now

try where? in the terminal or in the bash script?
sorry i dont understand

In the bash script. In the terminal it is working with just the command, but when you run a cronjob, it run’s from the users (root user) home directory, but the binary for shutdown is not there, it is in /usr/sbin.

ah ok. got it. i will try it after my web meeting im currently stuck in.

but shouldnt it be:

#!/bin/bash
sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sh -c "echo `date '+%s' -d '+ 600 minutes'` > /sys/class/rtc/rtc0/wakealarm"
/usr/local/bin/shutdown -h now

so with /usr/local/bin/ instead of your proposed /usr/sbin/ since the bash script is in following folder:

root@DietPi:~# cd /usr/local/bin
root@DietPi:/usr/local/bin# nano shutwake.sh

It’s a misunderstanding, we want the path where the shutdown binary is located in, not where shutwake.sh is.

Just do which shutdown and you get the path to the binary.
On my device it’s in /usr/sbin.

#!/bin/bash
sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sh -c "echo `date '+%s' -d '+ 600 minutes'` > /sys/class/rtc/rtc0/wakealarm"
/usr/sbin/shutdown -h now
2 Likes

THAT DID THE TRICK. Now it works.

bash-script:

#!/bin/bash
sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sh -c "echo `date '+%s' -d '+ 600 minutes'` > /sys/class/rtc/rtc0/wakealarm"
/usr/sbin/shutdown -h now

change minutes to your prefered time

then install a cronjob:

crontab -e -u root

and add:

05 00 * * * /usr/local/bin/shutwake.sh

change 05 00 * * * regarding your needs via Crontab.guru - The cron schedule expression generator*

1 Like

Good stuff! Way to go!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.