The use of tool crontab for timed tasks under Linux

Source: Internet
Author: User

Linux Planning task Tool cron Usage


Linux under the famous Planning task tool crontab introduction Baidu, Google on more people dazzling, the spirit of "the world article a big copy" of the Enlightenment, coupled with my daily work summary of the use of experience, simply launched this note-taking article, for forgetting when the use of the page.
Cron is the next program in the Linux system that automatically executes a specified task, which includes features such as "Time", "path", "Auto-execute script", for example, I want to copy the "new" directory under/usr/local/at 4 o'clock in the morning by automating the script "newcopy.sh" to/ The "Superman" directory under Home.
Cron program after the installation of the system will generally automatically start with the system, no need to start each time, if inadvertently it was canceled from the start, you can add it to the system from the boot list by NTSYSV or Chkconfig, as follows:

#ntsysv
Select Crond in the Self-launch list

First, cron service introduction

Cron Save the command line in the/etc/crontab file, each system user if set up their own cron, that will be in the/var/spool/cron under the name of the application crontab. Regardless of whether you write files in the/var/spool/cron directory or/etc/crontab files, you can make Cron perform the scheduled tasks accurately, except that the crontab files for each system user under/var/spool/cron are the task configuration of the corresponding user level. The/var/crontab file is a task configuration that corresponds to the system level. The cron server reads all the files and/etc/crontab files in the/var/crontab/cron directory every minute.


Ii. using the Cron service to perform scheduled tasks

#service Crond Start/restart/stop
Start/restart/Shut down cron service

#crontab-L
Browse the current user's crontab, which is the list of scheduled tasks that already exist

#crontab-E
Edit the current user crontab, such as never edited the crontab file before, then crontab is a blank file, we can completely manually write the list of scheduled tasks we need, the following detailed description of the scheduled task list.

Here are a few examples:

1) 9 9 * * */USR/LOCAL/BIN/CVSB
2) 3 3 * * 0/usr/local/bin/qbbak
3) * */6 * * */usr/local/bin/esbbak
4) 20,30 * * */usr/local/bin/esbak
5) * * * * 2-5/usr/local/bin/esbbak

The six fields correspond to the following meanings:
9 9 * * *
Minute hour date Month week

1) indicates that the CVSB file in directory/usr/local/bin/is executed 9:9 A.M. daily
2) indicates that the Qbbak file in directory/usr/local/bin is executed every Sunday 3:3 A.M.
3) is executed every 6 hours, execution time from the first execution of the calculation, of course, you can also customize the execution time, such as 0 */6 * * * * That program will be executed on the hour
4) indicates the execution at 20 minutes and 30 ticks per hour
5) indicates the execution of 23 points per day from Tuesday to Friday

The above is a brief introduction to some of the basic cron usage, the time combination of this stuff is not generally strong ~ can be complex to you cannot imagine, we can only grasp a little basic to meet the daily needs.


Third, Cron advanced use

1. Avoid cron generating junk files

Careful friends may find that the system/var/spool/clientmqueue/directory is often a large number of files, because the system has a user to open cron, and Cron executes the program has output content, the output will be sent to users of cron mail, The SendMail (the system comes with the mail server) did not start, so the files are generated.

You can then use output redirection, which is to add >/dev/null after the task is scheduled 2>&1

Cases:
9 9 * * */USR/LOCAL/BIN/CVSB >/dev/null 2>&1

2>&1: Redirect the error to where the output is to be sent.
REDIRECT the execution result of the command to/dev/null, which discards the resulting error.


2. A frequently encountered situation, the need to execute the file is not a problem, but through Cron is dead or alive can not be executed correctly, then we could output redirection to the cron execution process output to a file, through the analysis of the execution process to find the cause of the error

Cases:
9 9 * * */USR/LOCAL/BIN/CVSB >/var/log/crontab_log 2>&1

The above example will be the execution of cron output to a text file Crontab_log, this file can be arbitrarily built manually, put in any line, redirect directory write pair OK.

The use of tool crontab for timed tasks under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.