Automate tasks with crontab

Think about operation systems in computer, we immediately think to Windows and Linus. Every system also has its way to help developers manage their tasks. So, this concept of Windows is "Task Schedule" and Linus call "Cron".

Cronjob are commands that execute a preset action at a given time. Crontab is a storage place for Cronjobs. Follow Automate tasks with crontab to schedule your Cronjobs more convenient with Linus.

How to make Tasks with crontab

Setup

yum install cronie

Start crontab and automatically run every reboot

service crond start
chkconfig crond on

Some commonly used commands

crontab -e: create or edit crontab file 
crontab -l: show crontab file
crontab -r: remove crontab file

Structure of crontab file

It has 5 timed fields, finally the command will be run periodically, the structure is as follows:

*     *     *     *     *     command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

How to set automatic with Crontab

Run the script every 30 minutes

0,30 * * * * command

Run the script every 15 minutes

0,15,30,45 * * * * command

Run script at 3am every day

0 3 * * * command
@reboot  Run once every reboot
@yearly  Run once a year    "0 0 1 1 *"
@annually (Similar to @yearly)
@monthly Run once a month  "0 0 1 * *"
@weekly  Run once a week  "0 0 * * 0"
@daily  Run once a day    "0 0 * * *"
@midnight (Similar to @daily)
@hourly  Run once per hour    "0 * * * *"

View log

tail /var/spool/mail/[username]
vi /var/spool/mail/[username]

Fix error command not found /bash/sh

SHELL=/bin/bash

Lzdev always ready to support. Goodluck to you! Have fun with Crontab!