Home > English > Knowledge Base > Explore the crontab

Explore the crontab

 

What is Crontab?

Crontab is a widget software based on scheduled work and timing to practice commands in liunx system. People who construct and maintain the software environment use cron to organise their work (commands or shell scripts) in order to run periodically in a fixed time, day or a period

How to install (crontab is usually installed by default)

# yum install cronie -y

Activate and run together with the system

# service crond start
# chkconfig crond on

Crontab structure

*     *     *     *     *     <this command will run when the crontab work>
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- days of a week(0 - 6) (sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- days of a month (1 - 31)
|     +----------- hours(0 - 23)
+------------- minutes (0 - 59)

Common commands:

  1. Crontab –l                        :Check lists that crontab is currently running
  2. Crontab –u [name] –l         :Check who owns the lists that crontab is currently running
  3. Crontab –e                        :Make a crontab
  4. Crontab –u <name> –e      :Make a crontab with a userTạo một crondtab với user
  5. Crontab -r                         :Delete the crontab

Note :crontab uses Vi for drafting

Press 'a' to start drafting

Then press 'Esc' to quit darfting and press':wq' to save and quit

To check crontab activities

 # tail -f /var/log/cron

Crontab sends emails to cron job practicers by default, if you want to turn off this function, add this phrase at the end

>/dev/null 2>&1

Ex : 30 03 13 12 * rsync -avze ssh /source-folder/ [email protected]:/goal-folder/ >/dev/null 2>&1

Ex : we combine with Rsync

-Run the synchronizing command at 3.30 a.m on December 13th on any days of a week

30 03 13 12 * rsync -avze ssh /source-folder/ [email protected]:/goal-folder/

-If you want to run the command on many days or at many times, use the sign ',' between days and times, for example running on 6 and 12 o'clock, the 1st and 15th day

* 06,12 01,15 * * rsync -avze ssh /source-folder/ [email protected]:/goal-folder/

-If you want to make the time from 1 to 10 o'clock or from the 1st to the 10th day, use the sign '-' between them

* 01-10 01-10 * * rsync -avze ssh /source-folder/ [email protected]:/goal-folder/

-If you want to synchronize after every 10 minutes on any days, times and months, use the sign '/'

*/10 * * * * rsync -avze ssh /source-folder/ [email protected]:/goal-folder/