Abstract: The LinuxCron utility is an effective way to continuously schedule routine background jobs at a specific time and/or date. This article covers 15 good examples of crontab job scheduling.

This article is from “Linux Crontab: 15 Good Cron Examples in action” by Tiamo_T.

Experienced Linux system administrators know the importance of running routine maintenance tasks automatically in the background.

The Linux Cron utility is an effective way to continuously schedule routine background jobs at a specific time and/or date.

This article covers 15 good examples of crontab job scheduling.

Linux Crontab format

MIN HOUR DOM MON DOW CMD
Copy the code

1. Schedule homework for a specific time

The basic use of cron is to execute a job at a specific time, as shown below. This will execute the full backup shell script (full backup) on June 10 at 08:30 am. Note that the time field is in 24-hour format. So use 8 for 8am and 20 for 8pm.

30 08 10 06 * /home/ramesh/full-backup
Copy the code
  • 30-30 minutes

  • August — August morning

  • 10 — Day 10

  • 06 — 6th month (June)

  • * — Every day of the week

2. Schedule jobs for multiple instances (for example, twice a day)

The following script does incremental backups twice a day. This example executes the specified incremental-backup shell script at 11:00 and 16:00 every day. The comma-separated value in the field specifies that the command needs to be executed at all times mentioned.

00 dec 11 * * * / home/ramesh/bin/incremental backupCopy the code
  • 00 — Minute 0 (top of one hour)

  • 11,16 — 11 a.m. and 4 p.m

  • Every day * –

  • * — Every month

  • * — Every day of the week

3. Schedule work for a specific time frame (e.g. only on weekdays)

If you want to schedule assignments hourly within a specific time frame, use the following.

A Cron Job during daily working hours

This example checks the status of the database during business hours from 9 a.m. to 6 p.m., including weekends

00 09-18 * * * /home/ramesh/bin/check-db-status
Copy the code
  • 00 — Minute 0 (top of one hour)

  • 09-18 — 9 AM, 10 AM,11 AM, 12 AM,1 PM, 2 PM, 3 PM, 4 PM, 5 PM, 6 PM

  • Every day * –

  • * — Every month

  • * — Every day of the week

Cron Job Indicates the working time of each workday

This example checks the status of the database between 9am and 6pm during business hours every weekday, excluding Saturday and Sunday

00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
Copy the code
  • 00 — Minute 0 (top of one hour)

  • 09-18 — 9 AM, 10 AM,11 AM, 12 AM,1 PM, 2 PM, 3 PM, 4 PM, 5 PM, 6 PM

  • Every day * –

  • * — Every month

  • **1-5 -** Monday, Tuesday, Wednesday, Thursday and Friday (every business day)

4. How do I view Crontab entries?

View the Crontab entry of the current logged-in user

To view your crontab entry, type crontab -l from your Unix account, as shown below.

ramesh@dev-db$ crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space

[Note: This displays crontab of the current logged in user]
Copy the code

View the root Crontab entry

Log in as user root (su – root) and run crontab -l, as shown in the following figure.

root@dev-db# crontab -l
no crontab for root
Copy the code

Crontab HowTo: Displays the Crontab entries of other Linux users

To see crontab entries for other Linux users, log in to root and use **-u{username} -l**, as shown below.

root@dev-db# crontab -u sathiya -l 
@monthly /home/sathiya/monthly-backup 
00 09-18 * * * /home/sathiya/check-db-status
Copy the code

5. How do I edit the Crontab entry?

Edit the Crontab entry for the currently logged in user

To edit the crontab entry, use crontab -e, as shown below. By default, this will edit the currently logged in user crontab.

ramesh@dev-db$ crontab -e
@yearly /home/ramesh/centos/bin/annual-maintenance
*/10 * * * * /home/ramesh/debian/bin/check-disk-space
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C

[Note: This will open the crontab file in Vim editor for editing.
Please note cron created a temporary /tmp/crontab.XX... ]
Copy the code

When you save the above temporary file with :wq, it will save the crontab and display the following message indicating that the crontab has changed successfully.

~
"crontab.XXXXyjWkHw" 2L, 83C written
crontab: installing new crontab
Copy the code

Edit the root Crontab entry

Log in as user root (su – root) and run the crontab -e command.

root@dev-db# crontab -e
Copy the code

Edit Crontab file entries for other Linux users

To edit crontab entries for other Linux users, log in to root and use **-u{username} -e**, as shown below.

root@dev-db# crontab -u sathiya -e 
@monthly /home/sathiya/fedora/bin/monthly-backup 
00 09-18 * * * /home/sathiya/ubuntu/bin/check-db-status 
~ 
~ 
~ 
"/tmp/crontab.XXXXyjWkHw" 2L, 83C
Copy the code

6. Use Cron to schedule one assignment per minute.

Ideally, you might not need to schedule assignments every minute. But understanding this example will help you understand the others mentioned later in this article.

* * * * * CMD
Copy the code

* represents all possible units — that is, every minute of every hour throughout the year. In addition to using this * directly, you’ll find it useful in the following situations.

  • If you specify */5 in the minute field, it is every 5 minutes.

  • When you specify 0-10/2 in the minute field, it means every 2 minutes for the first 10 minutes.

  • Therefore, the above convention can be used for all the other four fields.

7. Schedule a background Cron job every 10 minutes.

If you want to check your disk space every 10 minutes, use the following method.

*/10 * * * * /home/ramesh/check-disk-space
Copy the code

It executes the specified command check-disk-space every 10 minutes throughout the year. But you may need to execute commands only during office hours and vice versa. The example above shows how to do these things. Instead of specifying values in five fields, we can specify it using a single keyword, as described below. In some special cases, you can replace the above five fields with @ followed by the keyword, such as restart, midnight, year, hour.

8. Use @Yearly to schedule work in the first minute of the year

If you want to perform a job in the first minute of each year, you can use the **@yearly** cron keyword, as shown below. This will perform annual system maintenance at 00:00 on January 1 of each year using the annual maintenance shell script.

@yearly /home/ramesh/red-hat/bin/annual-maintenance
Copy the code

9. Use @monthly to schedule Cron assignments that start each month

It is similar to @yearly above. But use the **@monthly** cron keyword to execute the command once a month. This will perform a shell script tape backup at 00:00 on the 1st of each month.

@monthly /home/ramesh/suse/bin/tape-backup
Copy the code

10. Use @Daily to arrange background assignments every day

Use the **@daily** cron keyword, which will perform daily log file cleanup at 00:00 every day using the cleanup-logs shell script.

@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"
Copy the code

11. How do I run the @reboot Linux command after each restart?

Use the **@reboot** cron keyword, which will execute the specified command once each time the machine is started.

@reboot CMD
Copy the code

12. How to disable/redirect Crontab MAIL output using the MAIL keyword?

By default, crontab sends the job to the user who scheduled it. If you want to redirect output to a specific user, add or update the MAIL variable in crontab, as shown below.

ramesh@dev-db$ crontab -l
MAIL="ramesh"

@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space

[Note: Crontab of the current logged in user with MAIL variable]
Copy the code

If you do not want to send MAIL anywhere, that is, to stop sending crontab output by email, add or update the MAIL variable in crontab, as shown below.

MAIL=""
Copy the code

13. How to use Crontab to perform a Linux Cron job every second?

You cannot schedule cronjobs every second. Because in CRon, the smallest unit you can specify is minutes. In a typical scenario, most of us have no reason to run any jobs on the system every second.

14. Specify the PATH variable in Crontab

In all of these examples we have specified the absolute path to the Linux command or shell-script that needs to be executed. For example, if you only want to specify tape-backup and not /home/rames/tape-backup, add the PATH /home/ramesh to the PATH variable in crontab, as shown below.

ramesh@dev-db$ crontab -l

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/ramesh

@yearly annual-maintenance
*/10 * * * * check-disk-space

[Note: Crontab of the current logged in user with PATH variable]
Copy the code

15. Install Crontab from the Cron file

Instead of editing the crontab file directly, you can add all the entries to the cron file first. Once you have all these entries included in the file, you can upload or install them to Cron, as shown below.

ramesh@dev-db$ crontab -l no crontab for ramesh $ cat cron-file.txt @yearly /home/ramesh/annual-maintenance */10 * * * *  /home/ramesh/check-disk-space ramesh@dev-db$ crontab cron-file.txt ramesh@dev-db$ crontab -l @yearly /home/ramesh/annual-maintenance */10 * * * * /home/ramesh/check-disk-spaceCopy the code

Note: This will install cron-file.txt into your crontab, which will also remove your old cron entries. So be careful when uploading cron entries from cron-file.txt.

Click to follow, the first time to learn about Huawei cloud fresh technology ~