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.
- Shutdown / Restart Ubuntu automatically using Crontab
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
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
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.
Please share if you like it on: