Scheduled jobs in Linux cron
Introduction
wiki
cron is a classic daemon (a computer program in UNIX-like systems)
used to run jobs periodically at a certain time.
Recurring actions are described by instructions placed in crontab files and
in special directories.
The name cron comes from the Greek word χρόνος (chronos) meaning time
(in English, chronos).
In systemd-based systems cron is effectively replaced by the built-in systemd.timer mechanism.
Instructions
The crontab files created for individual users are in the directory
/usr/spool/cron/crontabs/
or
/var/spool/cron/tabs/.
Editing them by hand is not recommended; the command used for that is
crontab -e
.
The files
crontab
, used to manage the whole system are located in the directory
/etc/cron.d/.
In addition, in the directories
/etc/cron.daily/, /etc/cron.weekly/ and /etc/cron.monthly/
automatically started programs are placed (daily, weekly or monthly).
Run the script every minute
crontab -e
*/1 * * * * /home/andrei/script.sh
Or simply
* * * * * /home/andrei/script.sh
Run the script every hour
crontab -e
0 * * * * /home/andrei/script.sh
This script will run every zero minute.
Run the script every day
crontab -e
0 1 * * * /home/andrei/script.sh
This script will run every day at one in the morning.
Run the script several times a day
The script will run at 7:00, 10:00 and 16:00
crontab -e
0 7 * * * /home/andrei/cronscript.sh > /dev/null 2>&1
0 10 * * * /home/andrei/cronscript.sh > /dev/null 2>&1
0 16 * * * /home/andrei/cronscript.sh > /dev/null 2>&1
This script will run every day at one in the morning. > /dev/null 2>&1 disables the sending of notification emails .
After a reboot
In the example the sshd service will be restarted after every reboot.
cd
touch restart_sshd.sh
echo "systemctl restart sshd" > restart_sshd.sh
crontab -e
@reboot /home/andrei/restart_sshd.sh
no crontab for andrei - using an empty one
crontab: installing new crontab
Running from other directories
By default cron runs scripts from the home directory
I have encountered cron not seeing scripts in the home directory unless a full path is
specified in crontab.
That is, such a job will not run
* * * * * script.sh
But this will work:
* * * * * /home/andrei/script.sh
To run a script from another directory, you need to go there first
* * * * * cd monitoring/new_scripts && $(which bash) script.sh
cron will go to the required directory and then, seeing &&, will understand that
the script also has to be executed (AND).
A logical or can be set with ||
A Python script on a schedule
To run a .py script from an arbitrary directory, go to
it and run $(which python3) the file name
For example:
crontab -e
* * * * * cd python_scripts && $(which python3) script.py
Disable email sending
To disable the email notifications that cron sends you, such as
You have new mail in /var/spool/mail/andrei
You need to redirect the output to /dev/null
For example:
crontab -e
* * * * * /home/andrei/script.sh > /dev/null 2>&1
Article author: Andrei Olegovich