YouTip LogoYouTip

Linux Comm Crontab

[![Image 1: Linux Command Manual](#) Linux Command Manual](#) Linux crontab is a command in Linux systems used to set up instructions that are executed periodically. After the operating system is installed, this task scheduling command is enabled by default. The **crond** command checks regularly every minute for tasks that need to be executed. If there are tasks to be performed, it will execute them automatically. **Note:** Newly created cron tasks will not execute immediately; it takes at least 2 minutes. Of course, you can restart cron to execute them right away. Linux task scheduling work is mainly divided into the following two categories: * **1. System tasks:** Periodic tasks that the system needs to perform, such as backing up system data, clearing caches. * **2. Personal tasks:** Regular tasks that a specific user needs to do, such as checking the mail server for new messages every 10 minutes. These tasks can be set up by each user individually. > Cron Expression Online Tool: [https://www.jyshare.com/front-end/9444/](https://www.jyshare.com/front-end/9444/) > > > Linux Cron Scheduled Tasks: [ ### Syntax crontab file or crontab { -l | -r | -e } **Explanation:** crontab is used to allow users to execute programs at fixed times or intervals. In other words, it is similar to a user's schedule. -u user specifies setting the schedule for a specific user. The prerequisite is that you must have the necessary permissions (e.g., root) to specify another user's schedule. If -u user is not used, it means setting your own schedule. **Parameter Description**: * -e: Execute a text editor to set the schedule. The default text editor is Vi/Vim. If you want to use a different text editor, please set the VISUAL environment variable first to specify which editor to use (e.g., setenv VISUAL joe). * -r: Delete the current schedule. * -l: List the current schedule. View the current user's crontab file: crontab -l Edit the current user's crontab file: crontab -e Delete the current user's crontab file: crontab -r List a specific user's crontab file (requires appropriate permissions): crontab -u username -l Edit a specific user's crontab file (requires appropriate permissions): crontab -u username -e * * * ## Format The time format is as follows: f1 f2 f3 f4 f5 program * Where f1 represents minutes, f2 represents hours, f3 represents the day of the month, f4 represents the month, and f5 represents the day of the week. program represents the program to be executed. * When f1 is *, it means execute program every minute. When f2 is *, it means execute program every hour, and so on. * When f1 is a-b, it means execute from the a-th minute to the b-th minute. When f2 is a-b, it means execute from the a-th hour to the b-th hour, and so on. * When f1 is */n, it means execute once every n minutes. When f2 is */n, it means execute once every n hours, and so on. * When f1 is a, b, c,..., it means execute at the a-th, b-th, c-th,... minute. When f2 is a, b, c,..., it means execute at the a-th, b-th, c-th,... hour, and so on. * * * * *- - - - -| | | | || | | | +----- Day of the week (0 - 6) (Sunday is 0)| | | +---------- Month (1 - 12) | | +--------------- Day of the month (1 - 31)| +-------------------- Hour (0 - 23)+------------------------- Minute (0 - 59) !(#) Users can also store all settings in a file first and use the command `crontab file` to set the execution time. | Time Setting | Meaning | | --- | --- | | `* * * * *` | Execute once every minute | | `0 * * * *` | Execute once at the 0th minute of every hour | | `0 0 * * *` | Execute once at midnight (0:00) every day | | `0 0 * * 0` | Execute once at midnight (0:00) every Sunday | | `0 0 1 * *` | Execute once at midnight (0:00) on the first day of every month | | `0 0 L * *` | Execute once at midnight (0:00) on the last day of every month | | `0 0 1 1 *` | Execute once at midnight (0:00) on January 1st of every year | | `0 0 * * 3` | Execute once at midnight (0:00) every Wednesday | | `0 0 1,15 * *` | Execute once at midnight (0:00) on the 1st and 15th of every month | | `0 0 * * FRI` | Execute once at midnight (0:00) every Friday | | `0 0 * * 5` | Execute once at midnight (0:00) every Friday | | `0 8-17 * * *` | Execute once every hour from 8:00 AM to 5:00 PM every day | | `0 12 * * MON` | Execute once at noon (12:00) every Monday | | `0 0 15 * *` | Execute once at midnight (0:00) on the 15th of every month | | `0 0 * * 3` | Execute once at midnight (0:00) every Wednesday | | `0 8-17 * * *` | Execute once every hour from 8:00 AM to 5:00 PM every day | | `0 0 * * 1-5` | Execute once at midnight (0:00) every weekday (Monday to Friday) | | `0 0 1 * FRI` | Execute once at midnight (0:00) on the first Friday of every month | | `0 0 1,15 * *` | Execute once at midnight (0:00) on the 1st and 15th of every month | | `0 0 15 1 *` | Execute once at midnight (0:00) on January 15th of every year | | `0 0 * * 7` | Execute once at midnight (0:00) every Sunday | | `0 0 * * 5` | Execute once at midnight (0:00) every Friday | ### Examples Execute /bin/ls once every minute: * * * * * /bin/ls In December, from 6:00 AM to 12:00 PM every day, execute /usr/bin/backup every 3 hours and 0 minutes: 0 6-12/3 * 12 * /usr/bin/backup Send an email to alex@domain.name at 5:00 PM every weekday (Monday to Friday): 0 17 * * 1-5 mail -s "hi" alex@domain.name /dev/null 2>&1` after a space at the end of each line, like this: 20 03 * * * . /etc/profile;/bin/sh /var/www/tutorial/test.sh > /dev/null 2>&1 ### Script Execution Issues If we use crontab to execute a script periodically but it fails to run, while executing it directly via a command (e.g., ./test.sh) works normally, this is mainly because the environment variables cannot be read. **Solutions:** * 1. All commands need to be written in absolute path form, e.g., /usr/local/bin/docker. * 2. Use the following code at the beginning of the shell script: #!/bin/sh. /etc/profile . ~/.bash_profile * 3. Add environment variables in **/etc/crontab**. Add the command `. /etc/profile;/bin/sh` before the executable command to make the environment variables effective. For example: 20 03 * * * . /etc/profile;/bin/sh /var/www/tutorial/test.sh [![Image 3: Linux Command Manual](#) Linux Command Manual](#)
← Linux Comm DeclareLinux Comm Clock β†’