Easy Tutorial
❮ Linux Comm Touch Linux Comm Rmt ❯

Linux crontab Command

Linux Command Manual

Linux crontab is a command used to schedule programs to be executed at regular intervals.

After installing the operating system, this task scheduling command is activated by default.

The crond command checks every minute if there are tasks to be executed and automatically runs them if there are.

Note: Newly created cron tasks will not execute immediately; they will start at least 2 minutes later. However, you can restart cron to execute them immediately.

Linux task scheduling is mainly divided into two types:

Syntax

crontab [ -u user ] file

or

crontab [ -u user ] { -l | -r | -e }

Explanation:

crontab is used to schedule programs to be executed at fixed times or intervals, similar to a user's timetable.

-u user specifies the timetable for a designated user, which requires the necessary permissions (such as root) to designate another user's timetable. If -u user is not used, it indicates setting up one's own timetable.

Parameter Explanation:

The time format is as follows:

f1 f2 f3 f4 f5 program

Users can also store all settings in a file and use crontab file to set the execution time.

Execution Time Format
Execute every minute * * * * *
Execute every hour 0 * * * *
Execute every day 0 0 * * *
Execute every week 0 0 * * 0
Execute every month 0 0 1 * *
Execute on the last day of every month 0 0 L * *
Execute every year 0 0 1 1 *

Examples

Execute /bin/ls every minute:

* * * * * /bin/ls

Within December, execute /usr/bin/backup every 3 hours from 6 AM to 12 PM:

0 6-12/3 * 12 * /usr/bin/backup

Send an email to [email protected] every day from Monday to Friday at 5 PM:

0 17 * * 1-5 mail -s "hi" [email protected] < /tmp/maildata

Execute echo "haha" at 0:20 AM, 2:20 AM, 4:20 AM, etc., every day:

20 0-23/2 * * * echo "haha"

Here are a few more specific examples: 0 */2 * * * /sbin/service httpd restart means restart apache every two hours.

50 7 * * * /sbin/service sshd start means start ssh service every day at 7:50.

50 22 * * * /sbin/service sshd stop means stop ssh service every day at 22:50.

0 0 1,15 * * fsck /home means check /home disk on the 1st and 15th of each month.

1 * * * * /home/bruce/backup means execute /home/bruce/backup file at the first minute of every hour.

00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \; means every Monday to Friday at 3:00, find and delete files named *.xxx in the /home directory that are older than 4 days.

30 6 */10 * * ls means execute ls command at 6:30 on the 1st, 11th, 21st, and 31st of each month.

Note: When a program runs at the specified time, the system sends an email to the current user with the content of the program execution. If you do not wish to receive such emails, add > /dev/null 2>&1 after a space in each line, for example:

20 03 * * * . /etc/profile;/bin/sh /var/www/tutorialpro/test.sh > /dev/null 2>&1

Script Not Executing Issue

If a script scheduled with crontab does not execute, but runs normally when executed directly (e.g., ./test.sh), this is mainly due to the inability to read environment variables.

Solution:

-

  1. Write all commands in absolute path form, such as: /usr/local/bin/docker.

-

  1. Use the following code at the beginning of the shell script:
    #!/bin/sh
    
    . /etc/profile
    . ~/.bash_profile
    

-

  1. Add environment variables in /etc/crontab, and add the command . /etc/profile;/bin/sh before the executable command to make the environment variables take effect, for example:
    20 03 * * * . /etc/profile;/bin/sh /var/www/tutorialpro/test.sh
    

Linux Command Manual

❮ Linux Comm Touch Linux Comm Rmt ❯