[WHAT IS CRONTAB?] – How to use CronTab Linux

How to use CronTab in VPS/Server using Linux operating system, will help you how to create and use in creating automation commands

1. What is CronTab

It is similar to Windows, Linux also has a way to create and run commands in a specified cycle.

In Windows it is called Task Schedule, and in Linux it is Cron.

Cronjob is a command that executes a preset action at given time. Crontab is the place to store cronjobs

Cron is a utility to help plan commands on the server side to execute one or more tasks at set time. Some people call them Cron jobs or Cron tasks.

Cron is a daemon program, it means that it is run underground forever once it is started up. Like daemons, you need to restart it if it has changes about setting up. This program looks at the setup file named crontab to execute the tasks described inside.

Normally, server administrators will often use this utility, but what about web developers? They sometimes need these scheduling functions. For example:

  • You can install so that cron job executes a scan to see if any trial users have been expired and delete or set inactive their account.
  • Send emails to users using the system daily or weekly …
  • Delete cache files monthly when they are too large
  • Check daily to see if website links are broken or not to repair quickly
  • Backup database

2. How does Crontab work?

A cron schedule is simply a text file. Each user has a separate cron schedule, this file is usually located /var/spool/cron. Crontab files do not allow you to create or edit directly with any text editor, unless you use the crontab command.

Some common commands:

– crontab -e: create and edit crontab file

– crontab -l: display crontab file

– crontab -r: delete crontab file

Almost all VPS/Server are installed crontab in advance, but there are cases where VPS does not exist. If you use the crontab -l  command, you see the output return -bash: crontab: command not found, you install crontab yourself.

3. Install crontab

Use the command:

 #yum install cronie 

Start crontab and automatically run whenever reboot:

 
#service crond start
#chkconfig crond on

4. Structure of crontab

A crontab file has 5 time-definite fields, finally the command will be run periodically, the structure is as follows:

If a column is assigned a * character, it means that the next task will be run at all values for that column.

For example:

– Run script every 30 minutes:

0,30 * * * * command

– Run script at 3 a.m every day:

0 3 * * * command

Particular example:

Suppose that I write a script to backup the whole folder /home/domain.com/public_html/ and move the .zip compressed file to the /root/ folder as follows:

#!/bin/bash

zip -r /root/backup_domain.com_$(date +”%Y-%m-%d”).zip /home/domain.com/public_html/ -q

This script saves in the path /etc/backup.sh (execute authorization – chmod +x if it is bash script).

Then I let this script run periodically at 15 p.m every Monday and Thursday by creating a crontab file as follows:

crontab -e

Press o (o) to add a new line with the content:

0 15 * * 1,4 sh /etc/backup.sh

To save and exit, you press ESC, then tyoe :wq press Enter.

Finally, remember to restart cron daemon:

/etc/init.d/crond restart

If you want to use the Editor nano to fix it easily, then you use the following command: EDITOR=nano crontab -e

The other examples:

– Let crontab run every a minute:

* * * * * sh /etc/backup.sh

– Let crontab run every 24 hours (at midnight):

0 0 * * * sh /etc/backup.sh

– Let crontab run PHP file every 24 hours:

0 0 * * * /usr/bin/php /var/www/html/reset.php

Disable email
By default, cron sends an email to the cron job executor, if you want to turn off this function of sending email, add the following thing at the end of the line

>/dev/null 2>&1

For example:

0 15 * * 1,4 sh /etc/backup.sh >/dev/null 2>&1

Create log file

To save log to file:

30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

We will be happy to hear your thoughts

Leave a reply

Unix
Logo