crontab LX job schedular
crontab syntax
#---------------- Minute (0 - 59)
| #------------- Hour (0 - 23)
| | #---------- Day of the Month (1 - 31)
| | | #------- Month (1 - 12)
| | | | #---- Day of the Week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * example-script.sh
example of a cronjob:
Min Hour Day Of Month Month Day of Week Command / Script
30 * * * 1-5 echo “Live Long and prosper \//”
The above cronjob would run every half an hour from Monday to Friday.
The 6 crontabs field & format :
Minute 0-59 ( means every minute at the start of each minute)
Hour 0-23 ( means every hour at the start of every hour)
Day of Month 0-31 ( means every day)
Month 1-12 ( means every month, you can use month names if you prefer)
Day of Week 0-7 (0 and 7 both mean Sunday, again you can use names – see below)
As an alternative to the six field crontab syntax , it is also possible to use :
@daily last | mail user@domain.com
@reboot - This runs the Cron job when the machine is started up or if the Cron daemon is restarted
@midnight - This runs the Cron job once a day at midnight, it’s the equivalent of 0 0 * * *
@daily - Does exactly the same as @midnight
@weekly - This runs a Cron job once a week on a Monday morning and is the equivalent of 0 0 * * 0
@monthly – This runs a Cron job once a month on the first day of every month at midnight and is the same as 0 0 1 * *
@annually – Runs a Cron job once a year at midnight on the first day of the first month and is the equivalent of 0 0 1 1 *
@yearly - This is the same as annually
If you have your scripts ready there are a number of directories you can drop your scripts into located in the /etc directory such as /etc/cron.daily
list all your cron jobs : crontab -l
zie cron jobs wm
the first five fields may contain one or more values, separated by a comma or a range of values separated by a hyphen.
- * - The asterisk operator means any value or always. If you have the asterisk symbol in the Hour field, it means the task will be performed each hour.
- , - The comma operator allows you to specify a list of values for repetition. For example, if you have 1,3,5 in the Hour field, the task will run at 1 am, 3 am and 5 am.
- - - The hyphen operator allows you to specify a range of values. If you have 1-5 in the Day of week field, the task will run every weekday (From Monday to Friday).
- / - The slash operator allows you to specify values that will be repeated over a certain interval between them. For example, if you have */4 in the Hour field, it means the action will be performed every four hours. It is same as specifying 0,4,8,12,16,20. Instead of asterisk before the slash operator, you can also use a range of values, 1-30/10 means the same as 1,11,21.
create a cron job
type crontab -e
you come into editing mode
choose an editor (use nano) (only needed once , choice will be saved)
type or paste your job
note : use space between the 5 digits fields , but a tab to seperate the command/script field
14 21 * * * /mnt/data/SYS/scripts/scripts.bash/2.maintenance/trim-fs.sh
save it : Ctrl+O (write Out command)
do not change the name of the proposed cron file , just accept it
sudo access will obviously not work when run without password
sudo sh ‘/mnt/data/SYS/scripts/scripts.bash/2.maintenance/maint-clean-Linux.sh’
switch first to root
sudo su -
then create the cron job
56 20 * * * /mnt/data/SYS/scripts/scripts.bash/2.maintenance/maint-clean-Linux.sh
logs
grep CRON /var/log/syslog ( no sudo required)
Jul 28 20:56:01 lapwm CRON[101232]: (root) CMD (/mnt/data/SYS/scripts/scripts.bash/2.maintenance/maint-clean-Linux.sh)
Jul 28 20:56:01 lapwm CRON[101231]: (CRON) info (No MTA installed, discarding output)
Jul 28 21:14:01 lapwm CRON[102979]: (ward) CMD (/mnt/data/SYS/scripts/scripts.bash/2.maintenance/trim-fs.sh )
Jul 28 21:14:01 lapwm CRON[102977]: (CRON) info (No MTA installed, discarding output)
(CRON) info (No MTA installed, discarding output)
=> there is output from your cron job but your server does not have a Message Transfer Agent installed to process the output into an email.
3 ways you can fix this error #TODO
1. Install an MTA like the popular postfix. This can be installed in most cases from a package manager. For example, on Ubuntu you may run:
sudo apt-get install postfix
If you select local installation, the output from your cron jobs will be relayed to a local “mailbox” that you can easily tail:
sudo tail -f /var/mail/< cronuser>
2. if you don’t care about cron emails, is to silence the error by disabling emails from your crontab.
The easiest way to do this for all cron jobs is to add this to the top of the crontab file: MAILTO=”“
3. simply to ignore this error. This is not a fatal error and does not impact the success of your cron job itself. With proper cron job monitoring you can trust your job is working correctly without manually reviewing output each time it runs.
other options
@reboot which will run the defined script or command when the system is rebooted.
as that user, run crontab -e, and add this line to the file:
@reboot screen -dmS ScreenName /path/to/your/script.sh
Using alternatives to cron for schedules
cron is one of the most common ways to run tasks on a recurring basis,
but it has its limitations and is often difficult to use.
you need to be familiar with operating a machine using the Linux terminal
cron is often high maintenance, requires manual up-keep, lacks logging, and there’s no central place to see all the cron jobs you have running.