3

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

2 Answers2

3

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

References:

0
  1. In Ubuntu 20.04 you can execute crontab -e, then enter your command at the bottom of the text lines.
    • use cron command syntax, it can be viewed at man cron.
  2. You can use the "Crontab Generator" on this url https://crontab-generator.org/ to calculate your command parameters.
FedKad
  • 10,515
johncli
  • 199
  • 1
  • 9
  • Thanks for the answer. I just added a PS note to my question. Was wondering if you could update your answer accordingly – An Ignorant Wanderer Sep 30 '20 at 16:19
  • i just looked up around some forums and i did not find any comments about not using cron, just few more Q&A and recommend to use it. – johncli Sep 30 '20 at 17:30