The guides on this website often require that commands are run on a regular basis or run when your machine starts up. This is very easily done with the command scheduling tool built into Linux. This is known as cron, or crontab (short for cron table, I believe). The easiest way I’ve found to add things to this is via Webmin as it lets you quickly select a time to run a command and enter the command via a convenient web interface. Here’s an easy guide I wrote previously on how to install Webmin.
However, if you want to do it from the command line then this is how to do it…
First we need to understand how the cron table is structured. If you run crontab -l you should get a list of the jobs currently scheduled on your system. Note that this will only show you the jobs associated with your currently logged in user.
To see ALL scheduled jobs you should run the following as root:
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
You’ll get an output which looks something like this but a bit longer as I’ve chopped mine off so you don’t see all my stuff!:
Notice the start of each line. This series of numbers and *’s will determine when your job runs. These entries can been amended using the following rules:
Entry Description Equivalent To @yearly (or @annually) Run once a year at midnight in the morning of January 1 0 0 1 1 * @monthly Run once a month at midnight in the morning of the first of the month 0 0 1 * * @weekly Run once a week at midnight in the morning of Sunday 0 0 * * 0 @daily Run once a day at midnight 0 0 * * * @hourly Run once an hour at the beginning of the hour 0 * * * * @reboot Run at startup @reboot
# * * * * * command to execute # ┬ ┬ ┬ ┬ ┬ # │ │ │ │ │ # │ │ │ │ │ # │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names) # │ │ │ └────────── month (1 - 12) # │ │ └─────────────── day of month (1 - 31) # │ └──────────────────── hour (0 - 23) # └───────────────────────── min (0 - 59)
You can open the /etc/crontab file and edit it directly but that is not best practice as you could make a mistake. To add stuff to cron you should use the command:
Then add your new command using the scheduling rules above in this format…
15 14 1 * * /path/to/script.sh
This would run /path/to/script.sh at 2:15pm on the first of every month. Or adding…
would run /path/to/script.sh every time you started the NAS.