Linux Automated tasks with cron

Cron is a daemon that can be used to schedule the execution of recurring tasks according to a combination of the time, day of the month, month, day of the week, and week.

Cron assumes that the system is on continuously. If the system is not on when a task is scheduled, it is not executed.

01. Installing cron :

By default it is installed and running in Red Hat , CentOS, but if it is missing in your distribution , you can install it easily.

yum -y install cronie

It will create some folders to easily run your scripts repeatedly :
/etc/cron.monthly/ : to run script monthly
/etc/cron.weekly/ : to run script weekly
/etc/cron.daily/ : to run script daily
/etc/cron.hourly/ : to run script hourly
/etc/crontab : the basic file to run our simple tasks at more specific timing by editing this file directly or using crontab command.

Default SHELL and PATH may depend on current system settings, in my case on CentOS is Bash.
MAILTO=USER to specify what user to send mail after successful or failed jobs
To change it edit /etc/crontab , here is an example from my server (vim /etc/crontab):

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
02. Running crond service:
systemctl enable crond
systemctl start crond
systemctl status crond
03. Scheduling tasks:

The syntax to create a task is five parameters for TIMING then USER Name then the Command to execute.
minute hour day month day-of-month USER Command-to-be-Executed

# 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

To add a new cron job you can use crontab which is interactive to enter your cron line in the correct syntax and press CTRL+D to finish ,
OR crontab -e to open a text file editor to write your cron lines , save and quit when finish the same way as you save and quit your editor (vim , nano , … ) ,
OR directly edit /etc/crontab file to append your cron lines and save and quit ,
OR create a new file in /etc/cron.d/ and write your cron lines , save and quit.

To list cron jobs for current user:
crontab -l

To list cron jobs for another user:
crontab -u USER -l

To delete all cron jobs in crontab:
crontab -r

To delete specific job in crontab:
open /etc/crontab using editor like vim OR by executing crontab -e and remove a job you want.

Examples:

We will use simple command for testing, but you are free to use what you need.

backup /etc/ folder to a tar file evey day at 6:15 AM :

[root@localhost ~]# crontab 
15 6 * * * root tar -cvf /root/etc.tar /etc/*
[root@localhost ~]# crontab -l
15 6 * * * root tar -cvf /root/etc.tar /etc/*
[root@localhost ~]#

running a script every dat at 01:00 AM:

[root@localhost ~]# crontab 
* 1 * * * root  sh /scripts/backup.sh
[root@localhost ~]# crontab -l
* 1 * * * root sh /scripts/backup.sh
[root@localhost ~]#

That is it, I hope it was simple and easy.
Enjoy !.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s