Question
How do I create a root cron job that runs a command or script at a certain time?
Answer
To create a root cron job, log in to SSH as the root user and run the following command to open the crontab editor:
# crontab -e
Then insert a new line containing the interval and the command to 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:
CONFIG_TEXT: 0 2 * * * command_tool --with-arguments
Note: 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 the Esc key
- Type
:wq - Press the Enter key
There's no need to restart the cron service; 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:
CONFIG_TEXT: 0 2 * * * command_tool --with-arguments > /dev/null 2>&1
Comments
0 comments
Article is closed for comments.