Running BASH scripts automatically with cron is relatively easy. One of the important points to consider before configuring cron is which user the script should be run as. Be sure to log into the terminal as that same user when creating the cron job.
See the currently configured cron jobs:
$ crontab -l
Edit the cron jobs:
$ crontab -e
Cron job definitions
.---------------- 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
Example cron jobs
Run a script every Tuesday at 1:05 AM
5 1 * * 2 /some_script.sh
Run a script everyday at 6 AM(6:00) and 6 PM(18:00)
0 6,18 * * * /some_script.sh
Run a script at the 17th minute of every hour, everyday
0 6,18 * * * /some_script.sh
Optional: Receive an email when a script is run
By default, sendmail is included un Ubuntu 18.04. The following example code can be added to the BASH script that will be run, to send yourself an email each time the script is run.
/usr/sbin/sendmail -i -t << EOF
From: cron_script <cron_script@{FQDN-of-Website}>
To: <example@test.com>
Subject: Script was run by cron
-----------------------------------------
CRON on {FQDN-of-Website} ran the script:
/some_script.sh
-----------------------------------------
EOF
Comments
0 comments
Article is closed for comments.