Scheduling reboots on Debian with cron

Cron lets you schedule tasks. It's very flexible, and is a great way to say.. schedule your backups to run, or a reboot to happen during off hours.

One thing to note is that whatever user you are logged in as when you enter cron is who that command will be run as, each user has their own cron file. So, if something you want to do requires root privileged, then you need to set that up under the root cron.

I'm going to show you how to setup a scheduled reboot at 11pm every sunday. First, this does require root, so lets sign in as root

sudo su root

Now, lets edit the cron file

crontab -e (note, crontab -l just shows the current items in cron, and is the prefered method of reading the cronfile if you don't intend to make any edits)

The current last line of the file tells you how your edits will be laid out it is

# m h dom mon dow

These stand for minute, hour, day of month, month, and day of week. Monday is 1, Sunday is 7. To setup something on 11pm every sunday you would do the following

0 23 * * 7 reboot

These don' have to be a single command either, they can be something like this

0 23 * * 7 sh /archive_last_backup.sh && sh /run_backup.sh && sh /transfer_backup_offsite.sh

Note that there is a space between each time argument, and that you can use some wild cards. * means every. */4 would mean every 4. 2,4,5,8 would mean on the 2nd, 4th, 5th, and 8th. 4-8 would mean on 4,5,6,7, and 8.

Have any more specific questions? let me know. I use crontab mainly to take scheduled backups of my site's database and backup those desktops via rsync to my home computer.

Add new comment