Schedule shutdown every day on Ubuntu 20.04

I'd like to schedule a shutdown every day at a specific time for my system. I found this post that addresses this, but I can't find rc.local I looked around and it seems like this file doesn't exist on Ubuntu 20.04 I was wondering what the best way to solve this problem is? Should I create this file or is there a file that "substitutes" it?

PS: currently the answers to my question mention cron. However, I found the accepted answer to this post that recommends not using cron for shutdown. I was wondering if someone could elaborate on this option too and why it is not / or is a good option


Solution 1:

With a quick search, I hopefully found the answer you're looking for

Automatic shutdown at specified times?

" Cron will work very well for this.

Add the below line (with tweaks) to the end of /etc/crontab:

30 23 * * * root shutdown -h now "

From: Automatic shutdown at specified times?

Reply if it worked! Good luck!

To run sample.sh twice a day. say 5am and 5pm

0 5,17 * * * shutdown.sh

where shutdown.sh has the contents

shutdown

  • Or, if you want another way:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
  0 0 * * 0 root      /sbin/shutdown -h now # power off every sunday at 00:00 am

Or, alternativly you can use:

sudo crontab -e

and enter 0 0 * * 0 root /sbin/shutdown -r now

For example

References:

https://unix.stackexchange.com/questions/208205/how-to-schedule-shutdown-every-day

https://www.unix.com/red-hat/173983-cronjob-command-shutdown.html

https://www.tutorialspoint.com/unix_commands/crontab.htm

https://help.ubuntu.com/community/CronHowto

https://www.ryadel.com/en/linux-auto-reboot-shutdown-cron-job-crontab-task-scheduler/

https://linuxize.com/post/cron-jobs-every-5-10-15-minutes/

shutdown 5:30pm every day

recurring scheduled shutdown