crontab-timed task commands in Linux

Source: Internet
Author: User

Linux server for a long time no contact, out of some new version, Ubuntu has been out of 12.10, before the understanding of Ubuntu only out of the desktop version of curiosity, can be as dazzling as Win7, now Ubuntu has officially entered the eyes of liunx enthusiasts, Application for large servers in the background, the main reason for selecting it is due to stability considerations. With the habit of CentOS, suddenly transferred to Ubuntu or some unaccustomed, after all, CentOS and the traditional Redhat no difference. However, the order is still the same, more use on the familiar.


About task Timing commands crontab, the application in Linux is common, this time in order to cooperate with the development of some ancillary functions, as well as some backup updates and other scripts, you need to crontab to complete, under Windows is a batch processing, Linux is just a few more commands. Let's take a look at Crontab's introduction:


The crontab command is commonly used in Unix-and Unix-like operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. Typically, crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be executed. This type of work is generally called cron jobs. The crontab file contains a series of jobs and instructions sent to the cron daemon. Each user can have their own crontab file, while the operating system holds a crontab file for the entire system, which is usually stored in subdirectories under/etc or/etc, and this file can only be modified by the system administrator. Each line of the crontab file follows a specific format, separated by a space or tab into several realms, each of which can place a single or multiple numeric values.


In Ubuntu, the crontab command is as follows:


-e means editing the current crontab

-L indicates that the list displays the current crontab task

-R means to delete the current user's crontab

-I gives prompt message when deleting crontab


Note that the user's crontab this representation is that everyone has their own crontab, so if you need to do global execution, you must customize the root user's crontab, non-global, according to the individual user's own crontab to execute, when performing the editing crontab operation, We need to set the editing tool, otherwise the default is Crontab edit, for the user who is accustomed to VI editing, we need to set the. Profile in the user directory, such as the root configuration file. prifile, the following directory:


The files with the. are hidden files, so you need ls-a to list them, vi. Profile adds the following configuration to the file:

Editor=vi; Export EDITOR

Indicates that the current root user uses VI as the default editor, and the CRONTAB-E is used directly to edit the task list as follows:


The above notation, which begins with the # sign, is the information for the comment description, and the red flag is the task list.


For Crontab's task configuration description;

Minute hour Sun Moon Week command

*    *    *  *  *    *

At a glance, the meaning of our red flag is to execute the script run.sh 3 o'clock every day and automatically output the execution log to the Log.log file.

*/1 * * * * root/opt/bin/test-cron.sh//Indicates that the script executes once every minute

Note: The crontab task is a cron process that is customized to run in the background of the system, so the user book is unable to see the cron process status. Set the boot cron process to start, with the following command:

Chkconfig--add Crond, add it to the list of services in the system.


Of course, crontab command with more, will more and more like it, because it can simplify our daily work, especially the database backup, detection server running state and some of the regular work, can be customized according to your needs, in a reasonable time to run the right task, do not affect the performance of the server , but also can save the usual working time, greatly improve the function efficiency.


Added: In the last two days found that a script in the Crontab timed task is not executed, other scripts can be executed normally, took a little time to debug, found some problems, or recorded, easy to read later. This script in the command line execution is OK, but just found in the crontab is not successful execution, and later through a series of validation, found that the original is the path of the problem, crontab is of course to use absolute path, so, crontab to execute the script must also use absolute path, Originally we run the script in a Java class using a relative path, so that the script is not executed, but the log file is created, so quickly can be positioned to script execution is not successful, the path is modified to an absolute path, after discovery execution, the Java class to identify the path as a package name, So finally you can only add the directory Switch command in front of the shell script, such as cd/root/... , so that script execution can be identified to the directory, and no matter where it is executed, it is an absolute path that can be found and run successfully. After modifying Crontab, remember to perform/etc/init.d/cron restart restart cron service.

More wonderful Linux video tutorials at 51CTO Academy: http://edu.51cto.com/course/courseList/id-48.html

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.