Cron¶
Cron is a special program responsible for executing tasks at a predetermined time. Each user has a special crontab
table where they can set up programs to run automatically.
DevilWEB¶
After logging in to the administrative panel, the Cron configuration can be found in the Cron jobs
tab. By clicking the Edit
button, a cron table will appear. On the right side of the entry, there is a button to enable or disable the entry, as well as a button to delete the task.
To add an entry, click + Add cron job
and fill in the fields on the page. For example, the script /usr/home/LOGIN/script.sh
will be run at 5:30 every Sunday
.
Command line¶
Managing crontab
tables is also possible from the command line using the crontab
command:
crontab -l
- shows the current cron tablecrontab -e
- edits the tablecrontab -r
- deletes the table
After running the crontab -e
command, the table will be opened in the default editor. To change the default editor, change the EDITOR
environment variable. For example, to change the default editor to nano
, run the following command: echo 'export EDITOR=nano' >> $HOME/.bashrc && source $HOME/.bashrc
. Adding export EDITOR=nano
to the /usr/home/LOGIN/.bashrc
file will cause the default editor to be permanently changed.
Examples¶
Here is an example crontab. Pay special attention to the PATH variable - it makes working with Cron more convenient.
# Use /usr/local/bin/bash to run commands. This line is not required for crontab to work.
SHELL=/usr/local/bin/bash
# Use the following value for the PATH variable. Without this, we must specify full paths to binaries
# (e.g. instead of python we must specify /usr/local/bin/python, etc.)
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/usr/home/LOGIN/bin
# Run 5 minutes after midnight, every day
5 0 * * * /usr/home/LOGIN/bin/daily.job >> /usr/home/LOGIN/tmp/out 2>&1
# Run at 14:15 on the first day of every month -- output is
# mailed to the owner of the crontab
15 14 1 * * /usr/home/LOGIN/bin/monthly
# Annoy Stepan every weekday at 22:00
0 22 * * 1-5 echo "Stepan, it's 22:00. Where are your kids?!" | mail -s "It's 22:00" stepan@domain
# Send mail (daily at midnight)
0 0 * * * echo -e "Subject: Title of mail\n$x" | /usr/sbin/sendmail address@email
# Other examples
23 0-23/2 * * * echo "run 23 min after midnight, 2, 4, ..., every day"
5 4 * * sun echo "Run at 5 past 4 on Sundays"
0 10 * * 1,3 echo "Run every Monday and Wednesday at 10.00"
*/2 * * * * echo "Run every 2 minutes"
# Or using the verbal format using the "@" symbol, i.e.:
@reboot echo "System has been rebooted!"; # - command run every time the system is rebooted
@yearly echo "Another year has passed!"; # - equivalent to "0 0 1 1 *"
@annually echo "Another year has passed!"; # - same as above
@monthly echo "Another month has passed!"; # - equivalent to "0 0 1 * *"
@weekly echo "Another week has passed!"; # - equivalent to "0 0 * * 0"
@daily echo "Another day has passed!"; # - equivalent to "0 0 * * *"
@midnight echo "Another day has passed!"; # - same as above
@hourly echo "Another hour has passed!"; # - equivalent to "0 * * * *"