How do I create a cron job that runs a command or script at a certain time?
To create a cron job, log into SSH as the root user and run the following command to open the crontab editor:
Then, insert a new line containing the interval and the command that should be run. In vi or vim, use the "i" key on your keyboard to enter "insert" mode, then press enter to create a new line. Using the arrow keys to place your cursor on that new line, type your new entry:
0 2 * * * command_tool --with-arguments
The interval contains 5 parts. Minute, hour, day, month, and day of the week. The '*' entry means "every", so the above example means "at 0200 every day".
After adding this line, you will need to save the entry.
If the editor is "vi," or similar:
- Press ESC
- Type ":wq"
- Press enter
There's no need to restart cron at this point, once you've saved the crontab, the cron is active.
If your command has any output, it will typically be sent to your root contact. To avoid this, add "> /dev/null 2>&1" to the end of your command to silence the emails.