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

An Ignorant Wanderer
  • 767
  • 3
  • 12
  • 27
  • Cron? Have a look here: [Nightly server shutdown and auto reboot with cron](https://askubuntu.com/questions/1210222/nightly-server-shutdown-and-auto-reboot-with-cron) – Artur Meinild Sep 30 '20 at 14:13
  • @Artur Meinild Thanks for the comment. 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:20
  • I just think cron is easier for this kind of thing. The other method might work as well, but I'm more familiar with cron. – Artur Meinild Sep 30 '20 at 16:24
  • You should still be able to add the following line in a crontab and get the same effect: `@reboot shutdown -h 22:00 &` – Artur Meinild Sep 30 '20 at 16:36
  • 1
    Does this answer your question? [Automatic shutdown at specified times?](https://askubuntu.com/questions/567955/automatic-shutdown-at-specified-times) – karel Oct 01 '20 at 03:36

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:

William Martens
  • 953
  • 4
  • 18
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
  • 9,212
  • 7
  • 40
  • 79
johncli
  • 189
  • 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