Skip to main content

CronJob - Schedule Tasks and Execute Jobs


    Linux CronJob tutorial
Cron is a deamon (program that runs as a background process) used to automatically execute scheduled jobs or scripts at regular and predefined intervals without user intervention. Cron is primarily use
d for jobs needing to be executed over and over like log rotation every week or a report email sent out every morning. 

Crontab (CRON TABle) is a simple ASCII text file which specifies jobs (cron entries) and times when specific taks is to be run. Eeach user has it's own cron schedule file. This can be very useful if we have some tasks that have to be run periodically at certain times or dates, so it can be used to automate system maintenance or administration. 

Similar tool called "at" can be used to execute jobs which are processed by "atd" daemon, but scheduled tasks are executed only once. 



Scheduling cron jobs
To schedule a cron job, user should simply edit a cron schedule file or crontabCrontab can be usually located in /var/spool/cron/crontabs/, but the file shouldn't be edited manually. To edit crontab, use "crontab -e" command. Each line in crontab file represents a crontab task or cron job. Lines that start with "#" sing are considered as comments and will not be executed. 

Each cron job has 6 sections separated by a single space. Sections 1-5 indicate when and how often by the local time the task (red command in the 6 section) will be executed: 














(If asterisk "*" sign is used, it indicates that every instance (i.e. every hour, every weekday, etc.) of the particular time period, command will be run) 

Editing users crontab with "crontab -e" command: 

LinuxBox# crontab -e # m h dom mon dow command 00 14,21,00 * * * /var/www/backup.sh >> /var/www/log/backup.log 2>&1 ... .. .
(Once edite, simply exit the crontab with CTRL-X and confirm writing the changes) 



    Cron job examples
Scheduling a backup script that will run every Sunday at 02:00 am: 

LinuxBox# crontab -e 00 2 * * 0 /var/www/backup.sh >> /var/www/log/backup.log 2>&1


Scheduling a backup script that will run every day at 02:00 am: 

LinuxBox# crontab -e 00 2 * * * /var/www/backup.sh >> /var/www/log/backup.log 2>&1


Scheduling a backup script that will run every day at 06:00 and 23:00: 

LinuxBox# crontab -e 00 6,23 * * * /var/www/backup.sh >> /var/www/log/backup.log 2>&1


Scheduling a backup script that will run on January 1st at 06:00 and 23:00: 

LinuxBox# crontab -e 00 6,23 1 1 * /var/www/backup.sh >> /var/www/log/backup.log 2>&1


Scheduling a backup script that will run every 15 minutes: 

LinuxBox# crontab -e 00,15,30,45,59 * * * * /var/www/backup.sh >> /var/www/log/backup.log 2>&1


( If email notifications about completed or failed cron jobs is not needed, " >/dev/null 2>&1 " syntax can be added at the end of each cron job command. This will simply redirect stdout and stderr in a black hole or a trash bin of sort ) 

Comments

Popular posts from this blog

Cisco three-layer hierarchical model

Because networks can be extremely complicated, with multiple protocols and diverse technologies, Cisco has developed a layered hierarchical model for designing a reliable network infrastructure. This three-layer model helps you design, implement, and maintain a scalable

Access Control Lists (extended)

Access Control List  or  ACL  is a technic of controling network traffic. It is a list of rules with which traffic flow can be manipulated - permitted or denied. By default, traffic flow from the interface with the higher security level (for example "inside" interface) to interface with the lower security level (example "outside" interface) is allowed by default, but the flow of traffic from "outside" to the "inside" interface must be explicitly permited. 

Classes of IP addresses

TCP/IP defines five classes of IP addresses: class A, B, C, D, and E. Each class has a range of valid IP addresses. The value of the first octet determines the class. IP addresses