About
The cron 1) is a scheduler daemon that gets its data from crontab files where each users have their own crontab.
Crontab 2) is the command line application that is used to install, deinstall or list the crontab files.
Example
- At 13h30 every Monday
30 13 * * 1
- Each first of month at 0h00
0 0 1 * *
Crontab
A crontab consists of lines defining:
- an environment setting
- or a job definition.
- or a comment (#)
A job definition line consists of:
- the first five fields specifying the scheduled time
- the 6de field being the command executed
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * command to be executed
How to show the Crontab for the current user
crontab -l
# weekly gather statistics
00 23 * * 3 /home/gerard/scripts/gather_statistics.sh > /dev/null
# run the script to load tables between 22:00 and 10:00
*/5 22-23 * * * /home/gerard/scripts/loadTables.sh > /dev/null
*/5 00-09 * * * /home/gerard/scripts/loadTables.sh > /dev/null
#the first load check just after 22:00
03 22-23 * * * /home/gerard/check_load_is_running.sh
03 00-09 * * * /home/gerard/check_load_is_running.sh
#the last load check just before 10:00
58 09 * * * /home/gerard/check_load_is_running.sh
It will list the jobs for the user who ran the command.
How to see the crontab for another user
Each user can have their own crontab.
To see the job of another user:
- -u option
crontab -u userName -l
# equivalent to
# cat /var/spool/cron/userName
- logged in as the other user
- or run it with sudo.
They are files in /var/spool/cron in the spool that are not intended to be edited directly.
How to write a cron Job definition
A cron job line definition follows the following structure:
minute hour day-of-month month day-of-week command
where:
- command is command passed to the SHELL
- The time field minute hour day-of-month month day-of-week can be
Field | Star | One Number | */n | List of number | Range of Value - |
---|---|---|---|---|---|
minute | * means every hour | 0 to 59 - 5 means on the 5th minute of the hour | */5 means every 5 minutes | 0,5,10,15,20,25,30,35,40,45,50,55 - Same as */5 (ie every 5 minutes) | 10-15 means every minutes between 10 en 15 |
hour | * means every hour | 0 to 24 - 4 means at 4 hour | */5 means every 5 hour | 0,5,10,15,20 means at 0, 5,10,15 and 20 hour | 5-10 means every hour between 5 and 10 |
day-of-month | * means every day of the month | 1 to 28/30 or 31 - 5 means the 5 of the month | */5 means every 5 day | 5,10,15,20 means at 5,10,15 and 20 day of the month | 5-10 means every day between the 5th and 10th day of the month |
month | * means every month | 1-12 - 5 means in May | */5 means every 5 month | 5,10 means in May and October | 5-10 means in May, June, July, August, September, October |
day of week | * means every day | 0-7 where 0 and 7 is Sunday - 5 means Friday | */5 means every Friday | 2,5 means Tuesday and Friday | 0-3 means Sunday, Monday, Tuesday, Wednesday |
How to set environment variable
Several environment variables are set up automatically by the cron%288%29 daemon from the global /etc/crontab
- LOGNAME and HOME are set from the /etc/passwd line of the crontab´s owner.
- HOME and SHELL may be overridden by settings them in the /etc/crontab; LOGNAME may not.
Otherwise, you may define environment variable in the user crontab
MY_ENV=MY_VALUE
For your information, you can create a job to see the environment with the command
env >> /tmp/envcron.log 2>&1
Example of output:
XDG_SESSION_ID=32656
SHELL=/bin/sh
USER=apps
PATH=/usr/bin:/bin
PWD=/home/apps
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/apps
LOGNAME=apps
XDG_RUNTIME_DIR=/run/user/1007
_=/usr/bin/env
When executing commands, any output is mailed to the owner of the crontab (or to the user specified in the MAILTO environment variable in the crontab, if such exists).
If MAILTO is defined (and non-empty), the mail is sent to the user so named. If MAILTO is defined but empty (MAILTO=“”), no mail is sent.
View (Hourly|Daily|Weekly|Monthly) Cronjobs
cd /etc/cron.(hourly|daily|weekly|monthly)/
ls -l
cat filename
View Software Specific Cronjobs
cd /etc/cron.d/
ls -l
cat filename
Create / Edit
The command below starts the editor with the crontab file of the user.
crontab -e
Test
You can test the scheduled script with the following command:
env -i yourScript
The env -i will forget the environment variables as the cron do. You still need to add the environment variable added to the cron configuration file. See the environment variable section .
History: See log
Job Log (Cron Logs)
- When executing commands, any output is mailed to the owner of the crontab (or to the user specified in the MAILTO environment variable in the crontab, if such exists).
- Any job output can also be sent to syslog by using the -s option.
On a systemd-based system, you can use the journalctl utility to see system logs, including logs from the cron daemon.
To see logs from e.g. the last hour, and only from the cron daemon:
journalctl --since "1 hour ago" -t CROND
Apr 28 11:40:01 beau.bytle.net CROND[1143]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Apr 28 11:50:01 beau.bytle.net CROND[3922]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Apr 28 11:53:01 beau.bytle.net CROND[5040]: (root) CMD (/user/bin/phpctl monitor)
Command History
tail -f /var/log/cron
cat /var/log/cron
# or
sudo cat /var/log/cron
Example: output with:
- a LIST: crontab -l
- a CMD: a crond job listed in the crontab that starts /root/certbot/renewal.sh
- a EDIT: crontab -e
Sep 8 09:12:45 server01 crontab[22032]: (root) LIST (root)
Sep 8 09:13:01 server01 CROND[22035]: (root) CMD (/root/certbot/renewal.sh)
Sep 8 09:14:01 server01 CROND[22312]: (root) CMD (/root/certbot/renewal.sh)
Sep 8 09:15:01 server01 CROND[22589]: (root) CMD (/root/certbot/renewal.sh)
Sep 8 09:16:01 server01 CROND[22782]: (root) CMD (/root/certbot/renewal.sh)
Sep 8 09:17:01 server01 CROND[22970]: (root) CMD (/root/certbot/renewal.sh)
Sep 8 09:17:05 server01 crontab[22967]: (root) BEGIN EDIT (root)
Sep 8 09:17:08 server01 crontab[22967]: (root) REPLACE (root)
Sep 8 09:17:08 server01 crontab[22967]: (root) END EDIT (root)
Sep 8 09:18:02 server01 crond[519]: (root) RELOAD (/var/spool/cron/root)
Service
How to check the cron service
with Systemctl
systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-11-04 09:45:20 CET; 6 months 23 days ago
Main PID: 10103 (crond)
CGroup: /system.slice/crond.service
└─10103 /usr/sbin/crond -n
How to configure the cron service
with Systemctl with its unit file
cat /lib/systemd/system/crond.service
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target
[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
How to configure environment variables for all users (/etc/crontab)?
- For all users:
# the system crontabs file
cat /etc/crontab
# use /bin/sh to run commands, no matter what /etc/passwd says
SHELL=/bin/bash
# use the following path
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# mail any output to 'root'
MAILTO=root
Documentation / Reference
man crond # the daemon documentation
man crontab # the client