ONE NET WIKI Free and professional IT Wiki

How to use crontab to schedule a task on Linux OS

2018-10-29 ONE NET WIKI

111.jpg

Just like the Task Scheduler on Windows, the cron daemon on Linux runs tasks in the background at specific times. Add tasks to your system’s crontab files using the appropriate syntax and cron will automatically run them for you.

Crontab files can be used to automate backups, system maintenance and other repetitive tasks.


Step 1: Open a Terminal Window (Command Line) in Linux

1.png


Step 2: Open crontab

Use the crontab -e / sudo crontab -e command  to open your user account’s crontab file.

You may be asked to select an editor: nano, vi or vim.

3.png


Step 3: Add a new cron job to crontab

# run a backup of all your user accounts at 5 am every week
0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

4.png

Use comma-separated values to specific multiple times: 

# runs /usr/bin/example at the 15-minute mark on every hour, every day
0,14,29,44 * * * * /usr/bin/example

Cron command syntax

minute(0-59) hour(0-23) day(1-31) month(1-12) weekday(0-7) command

1. The number of minutes after the hour (0 to 59)

2. The hour in military time (24 hour) format (0 to 23)

3. The day of the month (1 to 31)

4. The month (1 to 12)

5. The day of the week (0 or 7 is Sun, or use name)

6. The command to run 

crontab.JPG


List existing cron jobs

crontab –l


Remove an existing cron job

1. Enter: crontab –e

2. Delete the line that contains your cron job

3. save the crontab file.


Check the status of cron jobs

On some Linux  distributions:

service crond status
service crond stop
service crond start

On other Linux distributions:

/etc/init.d/cron status

Linux-Shell-Scripting-29-crontab-and-at-jobs.png

Editor's Note: All the articles in this website are original. If reproduced, please indicate the source and link.
434
0
All Comments (0)
Expand to publish a comment

Web Development


Web Management


Web Marketing


Web Security