Often times one may choose to run a command line process or script at a specific time and date. Two commands that one might consider for this purpose are At and Crontab respectively, with the latter being the more complex of the two.
At is a relatively straightforward affair, that allows one to execute one or more commands on a single date and time. Many systems come with At installed, but some, such as Raspbian, a flavour of Debian designed to run on the Raspberry Pi, do not. To begin enter a command as thus:
This will then put you into an At prompt. Enter a command at the prompt. When you are finished typing the first command hit enter. If you wish to type another command do so now. If not type control+d to finish. It will add a
In the example a new empty file named “file.txt” will be created in the /home/admin directory at 5:00 PM on Tuesday. There is also a way to use At without entering its command prompt. You can use At as a single line command by piping the output from an echo statement through it.
Like the At command, Crontab allows one to dictate when a process or script is executed, but rather than simply running once, it runs repeatedly at a specified interval. This is particularly useful for maintenance tasks such as running backups, or monitoring tasks such as tracking system load averages.
When Crontab is invoked for the first time, a new crontab file is created and opened, for the current user, in the systems default text editor. Each line is separate scheduled event, with timing based on 6 values in order: Minute (0-59), Hour (0-23), Day of the Month (1-31), Month of the Year (1-12), day of the Week (1-7), and Year (1900+) respectively. In the case of Day of the Week it is important to note that the week in this case starts on Monday. To invoke crontab, use the following command:
To run a backup script once every Sunday at 2am one might ad a line as follows:
Both At and Crontab can be configured in many more complicated and useful ways. To learn more check out their Man pages.