IT/Software/System Config/cron: Difference between revisions
mNo edit summary |
|||
(12 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
== About == | ==About== | ||
Cron is a "daemon to execute scheduled commands"<ref>http://man7.org/linux/man-pages/man8/cron.8.html</ref> | Cron is a "daemon to execute scheduled commands"<ref>http://man7.org/linux/man-pages/man8/cron.8.html</ref> | ||
== Usage == | ==Usage== | ||
To edit your crontab you can run | To edit your crontab you can run | ||
crontab -e | crontab -e | ||
The first time you will need to select an editor. We normally use Nano the first choice. | |||
=== Formatting === | If you make a mistake you can run the <code>select-editor</code>(or <code>sudo select-editor</code>) command to change it. | ||
To edit root privilege crontabs you need. | |||
sudo crontab -e | |||
===Formatting=== | |||
Cron jobs are in typically in the following format | |||
* * * * * /path/to/script.sh | * * * * * /path/to/script.sh | ||
The time is in the format of | The time is in the format of | ||
Line 24: | Line 30: | ||
*/5 * * * * /path/to/script.sh | */5 * * * * /path/to/script.sh | ||
=== Special | ===Special Times=== | ||
Cron also has several special times that can be used in place of the regular time format. | Cron also has several special times that can be used in place of the regular time format. | ||
For the following, the script will be run on the first minute of the selected time frame. | For the following, the script will be run on the first minute of the selected time frame. | ||
* @yearly | *@yearly | ||
* @monthly | *@monthly | ||
* @weekly | *@weekly | ||
* @daily | *@daily | ||
* @hourly | *@hourly | ||
So, for example, to run at minute 0 of every hour we can do the following. | So, for example, to run at minute 0 of every hour we can do the following. | ||
Line 39: | Line 45: | ||
We also have one more special time to run scripts at reboot. | We also have one more special time to run scripts at reboot. | ||
* @reboot | *@reboot | ||
===Examples:=== | |||
We set computers to power off at 6 PM with this tab | |||
<code>0 18 * * * systemctl poweroff</code> | |||
<br /> | |||
=== Email notifications === | |||
To receive emails from cronjobs, first install a sendmail client. [[It/Software/System Config/SSMTP|SSMTP]] recommended. (sudo apt install ssmtp) | |||
Use "MAILTO" inside any cron file to declare the address to send output to. If no MAILTO is defined, cron will by default send all output to "username@hostname" on the sendmail configuration. If sendmail is configured to use the msgeducation mailserver, all emails will be redirected to admin@msgeducation.com with the To address as the username. To choose an address to send output to systemwide, put the following in /etc/crontab: | |||
MAILTO=it@msgeducation.com | |||
Note that you will receive an email for any cronjob that produces any output. This may mean you start to receive an email every minute for some job running somewhere. See the email to find which job it is, or find it manually. To silence a job, make sure it does not produce output by redirecting it to a log file or to /dev/null | |||
* * * * * command >/dev/null 2>&1 | |||
Leave out "2>&1" if you want to receive stderr but not stdout. | |||
==External Tools== | |||
[https://crontab.guru/ crontab guru] is a helpful tool for confirming your cron formatting is correct. | |||
<references /> |
Latest revision as of 14:21, 3 May 2024
About
Cron is a "daemon to execute scheduled commands"[1]
Usage
To edit your crontab you can run
crontab -e
The first time you will need to select an editor. We normally use Nano the first choice.
If you make a mistake you can run the select-editor
(or sudo select-editor
) command to change it.
To edit root privilege crontabs you need.
sudo crontab -e
Formatting
Cron jobs are in typically in the following format
* * * * * /path/to/script.sh
The time is in the format of
Minute, Hour, Day of the month, Month, Day of the week.
The following line would execute script.sh at minute 0 of the 8th hour, every day, every month and every day of the week.
Or in simpler terms, it runs every day at 8:00 AM.
0 8 * * * /path/to/script.sh
Now let's say we want to run our script every 5 minutes.
We could try doing the following but that would only run the script 5 minutes after every hour.
5 * * * * /path/to/script.sh
In cron we can use the "/" character to indicate step values so to run every 5 minutes we can do the following.
*/5 * * * * /path/to/script.sh
Special Times
Cron also has several special times that can be used in place of the regular time format.
For the following, the script will be run on the first minute of the selected time frame.
- @yearly
- @monthly
- @weekly
- @daily
- @hourly
So, for example, to run at minute 0 of every hour we can do the following.
@hourly /path/to/script.sh
We also have one more special time to run scripts at reboot.
- @reboot
Examples:
We set computers to power off at 6 PM with this tab
0 18 * * * systemctl poweroff
Email notifications
To receive emails from cronjobs, first install a sendmail client. SSMTP recommended. (sudo apt install ssmtp)
Use "MAILTO" inside any cron file to declare the address to send output to. If no MAILTO is defined, cron will by default send all output to "username@hostname" on the sendmail configuration. If sendmail is configured to use the msgeducation mailserver, all emails will be redirected to admin@msgeducation.com with the To address as the username. To choose an address to send output to systemwide, put the following in /etc/crontab:
MAILTO=it@msgeducation.com
Note that you will receive an email for any cronjob that produces any output. This may mean you start to receive an email every minute for some job running somewhere. See the email to find which job it is, or find it manually. To silence a job, make sure it does not produce output by redirecting it to a log file or to /dev/null
* * * * * command >/dev/null 2>&1
Leave out "2>&1" if you want to receive stderr but not stdout.
External Tools
crontab guru is a helpful tool for confirming your cron formatting is correct.