Linux Crontab uses the basic tutorial

  

cron from the Greek word chronos (meaning "time"), is the next program in the Linux system to automatically perform the specified tasks. For example, if you want to create a backup of certain files or folders during your sleep every night, you can use cron to automate it.

Starting and stopping the service

The cron service is a built-in service for linux, but it won't boot automatically. You can start and stop the service with the following command:

/sbin/service crond start
/sbin/service crond stop
/sbin/service crond restart
/sbin/service crond reload

The above 1-4 lines are the start, stop, restart service and reload configuration.

To set cron to start automatically at boot time, add /sbin/service crond start to the /etc/rc.d/rc.local script.

View, Edit, and Delete

cron saves the command line in a crontab (cron table) file, which is usually in the /etc directory. Each system user can have their own crontab (under /var/spool/cron/). To view the current user's crontab, type crontab -l; to edit the crontab, type crontab -e; to delete the crontab, type crontab -r. If you are currently root, to view/edit/delete/a user's crontab, just add -u USERNAME (such as crontab -e -u USERNAME) to the appropriate command. The default editor for crontab files is vi, you can type export VISUAL=’editor’ to change the default editor.

The cron service not only reads all the files in the /var/spool/cron directory every minute, but also needs to read the /etc/crontab file once. Configuring this file also allows cron to perform tasks. Using the crontab command is a configuration of user-level tasks, while editing the /etc/crontab file is a configuration for system-level tasks.

Syntax Description

The following are examples of two cron statements (in the /etc/crontab file). The former is used to back up the /etc directory at night, and the latter runs the Analog program to process server statistics.

12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1
52 5 * * * root /usr/local/src/analog-5.32-lh/analog >> /dev/null 2>&1

The following fields and fields in the cron statement:

Description 1 minute (0-59) 2 hours (2-24) 3 Date (1-31) April (1-12; or English abbreviation Jan, Feb, etc.) 5 weeks (0-6, 0 for Sunday; Or the word abbreviation Sun, Mon, etc. 6 User name (as the user when executing the command) 7 Command to execute (path)

Now look at the first line:

12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2&g t;&1

This statement will run tar czf /usr/local/backups/daily/etc.tar.gz /etc at 3:12 am (03:12) command. >> /dev/null 2>&1 means to send all standard output to /dev/null (linux recycle bin) and standard error output (2) to the same place as standard output (1) ( That is, /dev/null). Running this line of commands will not produce any output.

This statement can be a bit more complicated:

30 15 13 6 1 * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> ; /dev/null 2>&1

It will run tar czf /usr/local/backups/daily/etc.tar.gz /etc on Monday, June 13th at 15:30
command.

The following statement can achieve the same effect:

30 15 13 Jun Mon * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /Dev/null 2>&1

If you want to run a program as user joey 15 minutes per hour
, you can use:

15 * * * * Joey /usr/bin/somecommand >> /dev/null 2>&1

The asterisk (*) is a wildcard, indicating that cron will ignore this field.

If you want to run a program every two hours
, you can use */2 in the hour field. It will run at 2 o'clock, 4 o'clock, 6 o'clock … … 22 o'clock, 24 o'clock. The specific statement is as follows:

0 */2 * * * joey /usr/bin/somecommand >> /dev/null 2>&1

The cron can also use a comma (,) To specify multiple times. For example, if you want to run a program at 15 points and 30 minutes per hour
, you can use 15,30 in the minute field:

15,30 * * * * joey /usr/bin /somecommand >> /dev/null 2>&1

If you want to run a program at the first week of the month (ie 1 to 7) at the specified time of the day
, you can use 1-7 in the date field:

15,30 */2 1-7 * * joey /usr/bin/somecommand >> /dev/null 2>&1

This statement will run 15 minutes and 30 minutes every 2 hours on the 1st to 7th of each month (02:15,02:30……22:15,22:30, etc.) /Usr/bin/somecommand command.

If you want to execute a script collection in 16:18
every day, you can put all the scripts to be executed into a directory (such as /home/username/cron), you can use :

18 16 * * * root run-parts /home/username/cron >> /dev/null 2>&1

If you want to save the output of a program, you can Replace >> /dev/null 2>&1 with >> /home/user/somecommand.log 2>&1 .

Summary

  • View the current user's cron configuration, use crontab -l
  • Edit the current user's cron configuration, use crontab -e
  • Delete the current user's cron configuration, use crontab -r
  • View/edit/delete a user's cron configuration as root, add -u USERNAME
  • to configure system-level tasks after the command , edit the /etc/crontab file


  • Copyright © Windows knowledge All Rights Reserved