How to use crontab in Linux

Cron is the system process that will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule. Crontab in Linux is the program used to edit, remove or list the tables used to drive the cron daemon. cron jobs can be allowed or disallowed for individual users, as specified in the files cron.allow and cron.deny, located in the directory /etc. If the cron.allow file exists, a user must be listed there in order to be allowed to use a given command. If the cron.allow file does not exist but the cron.deny file does, then a user must not be listed there in order to use a given command. If neither of these files exists, only the superuser will be allowed to use a given command.

Syntax:
crontab [-u user] [-l | -r | -e] [-i] [-s]

Example:
Let’s say you have a script that backs up important files, or creates a report about system statistics, for example. Let’s say the script is called /tmp/linuxinanutshell/scripts/do-every-day.sh, and you want to run it every morning at 5 A.M.

To edit the crontab, use this command:
crontab -e

This will open the crontab in a text editor (Usually this is vi or vim, but it may be something else depending on your Linux distribution). Save the below entry in opened file:

0 5 * * * /tmp/linuxinanutshell/scripts/do-every-day.sh

Cron Command Format

Each cron command in the crontab file has five time and date fields, followed by a user name if it is the system crontab file, followed by a command. Commands are executed by cron when the minute, hour, and month of year fields match the current time, and at least one of the two day fields (day of month, or day of week) match the current time.

FieldAllowed values
minute0-59
hour0-23
day of month1-31
month1-12 (or names)
day of week0-7 (0 or 7 is Sun, or use names)

Refer other examples

#This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m
00 09-18 * * * /tmp/linuxinanutshell/scripts/db-status

#This example will check the disk space every 10 minutes.
*/10 * * * * /tmp/linuxinanutshell/scripts/disk-check.sh

#This example will execute the shell script tape-backup at 00:00 on 1st of every month.
@monthly /tmp/linuxinanutshell/scripts/backup.sh

You may want to see
How to sort contents of a file

The sort command is used to sort the contents of a file. You can sort the data in a text Read more

How to use sed command in Linux

sed, short for "stream editor", allows you to filter and transform text. A stream editor is used to perform basic Read more

How to extract archive file in Linux

To extract archive file in Linux, you can use the tar command, used for extracting content from the archive files. "tar" Read more

How to Change permission of a file in Linux

chmod command is used to change the permission of a file in Linux. In Linux, there is a set of rules for Read more

Leave a Reply

Your email address will not be published. Required fields are marked *