Schedule Tasks to Automatically Shutdown / Restart Ubuntu 12.04 (Precise Pangolin)

This brief tutorial is going to show you how to use Crontab in Ubuntu to execute scheduled tasks. Crontab is a file in most Linux systems that holds scheduled tasks that are automatically run or executed at a given date and time. You can add as many tasks you want and when the date and time come, they will automatically be processed. So, if you want to schedule a task so that your computer automatically shutdown or restart at midnight, then this tutorial is going to show you how to do that in Ubuntu using the Crontab file.

Objectives:

  • Shutdown / Restart Ubuntu automatically using Crontab
  • Enjoy!

To get started, press Ctrl – Alt – T on your keyboard to open Terminal. When it opens, run the commands below to open Crontab file.

sudo gedit /etc/crontab

 

crontab_precise

 

Next, add the line below at the end of the file and save. This line below will shutdown your computer at midnight everyday.

00  0   * * *    root   poweroff

 

crontab_precise_1

 

Here are few things to remember about what we added in the file.

  • The time format is military or 24 hours format ( 0  — 23)
  • The * means all legal values.    (ex. if you want the task to run everyday of the week, put * in dow column)
  • If you want a task to run on multiple days of the week, then use this format in the dow column:  1,3,5 (Monday, Wednesday, Friday).
  • If you want a task to run on the 10th of every month, put 10 in the dom column.
  • If you want a task to run only in January, February and March, put 1,2,3 in the mon column.

 

min hour day/month month day/week user commands 00 0 * * * root poweroff

30 12 * * 5 root reboot

 

The first line above power down your computer everyday at midnight, of every week of every month.

The second line reboot your computer at 12:30 PM of everyday of the month or every month and only on Friday.

 

So you can add as many tasks are you want.

Enjoy!

Please share if you like it on:



4 Comments

    • It might be a little tricky!
      As far as I know, the pidof would have to determine the PID of VLC and then kill it with “kill pidof vlc”.
      Thats the tricky part, since the PID is changing every time you start VLC. Don’t know if you can put the variable into the command.
      There are probably other ways, but this is the way that comes to mind.

  1. Just what I was looking for. TNX
    But, there might be something missing…
    Linux OS’s usually run UTC as system time.
    So, if we insert the local time, is it going to be offset by whatever the offset to UTC is?
    I use the BIOS to start the box automatically and didn’t think of that, so it most certainly started at a time I would at least expect it.
    Does, crontab adjust to the local time or not?
    Thanks again!

  2. Very helpful, thanks!
    Is there a way to suspend the shutdown task, if there is a specific program running?
    For example, I have a weekly backup and a monthly backup scheduled. I don’t want the computer to run 24/7. Can I say, if the backup software is running, don’t do it?

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *