Crontabs

Automating execution of repeated tasks by cron. Cron is not complicated when you get graps of the syntax -it's actualluyeasy to use. Check that you have cron (usually you have it) - if not do install it. # sudo apt install crontabs -y

Crontabs
Photo by Markus Spiske / Unsplash

Automating execution of repeated tasks by cron.

Cron is not complicated when you get graps of the syntax -it's actually easy to use.

Check that you have cron (usually you have it) - if not do install it.
# sudo apt install crontabs -y

# systemctl enable crond.service
# systemctl start crond.service

The main cron configuration file is /etc/crontab. Besides the cron file, you can run jobs from the following directories: /etc/cron.daily, /etc/cron.weekly, /etc/cro.nmontly.  Just put a script file in one of the directories to run it according to the schedule.
You can restrict access to the scheduler using /etc/cron.allow and /etc/cron.deny. It is enough to create these files and add users to them, who are allowed or denied to run cron tasks.

You can edit the crontab file  by crontab -e. This file is used by root or to configure system tasks.

Personal user files of cron jobs are stored in the /var/spool/cron/ .

The cron deakon scans every minute these  for tasks to perform:
/etc/crontab, /etc/cron.*/  and /var/spool/cron/

Cron commands

The structure of the command is as follows

To check your cron tasks use: # crontab -l or  # cat /var/spool/cron/root

minutes hours day_of_a_month months week_day
# Example of job definition:
# .---------------- 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
# | | | | |
# * * * * * user-name command to be executed

You can also use these commands

* 	 	 	means any
,        	to separete tasks (1,15 run 1st and 15 day of the month)
-        	range run weekdays 1-5
/4 	 	repete every 4 months or hours 
@reboot 	Runs once at boot 	
@yearly 	Once a year 	0 0 1 1 *
@annually	Once a year 	0 0 1 1 *
@monthly 	Once a month 	0 0 1 * *
@weekly 	Once a week 	0 0 * * 0
@daily 	 	Every day 	0 0 * * *
@hourly 	Every hour 	0 * * * *
@midnight 	At midnight 	

Cron logs

To track cron jobs or errors, you can view the log file: /var/log/cron. This file records all tasks and errors in the daemon operation if any.

Cron notifications

  1. First cron liks to send emails. If you want this install sendmail and configure it in the cron file.
  2. Send status to the log-file. * * * * * echo "Cron task" >> /var/log/cron.log
  3. Run in silent mode . * * * * * echo "Cron task" >> /dev/null 2>&1

Use of sendmail.

Let’s configure the parameters to send emails in the cron file. Add the following lines to the file: SHELL — the user shell and  HOME — the path to the cron file

MAILTO="[email protected]"
SHELL=/bin/bash
HOME=/
* * * * * echo "Cron task"

Photo by Possessed Photography / Unsplash--