Crontab command usage in Linux

Source: Internet
Author: User


The crond resident command for task scheduling crond is the command for linux to regularly execute programs. After the operating system is installed, the task scheduling command is started by default. The crond command periodically checks whether there is any job to be executed every minute. If there is any job to be executed, the job is automatically executed. For linux task scheduling, there are two main types of jobs: 1. jobs executed by the system: jobs to be executed by the system periodically, such as backing up system data and clearing the cache. 2. jobs executed by individuals: for example, you can check whether there are new emails on the email server every 10 minutes. Each user can set Crontab as a scheduled task trigger in the UNIX system, user Permissions are recorded in the following two files: file meaning/etc/cron. deny the users listed in this file are not allowed to use the Crontab command/etc/cron. allow users listed in this file are allowed to use the Crontab command/var/spool/cron/is the crontab file of all users/var/spool/cron/crontabs the Crontab command is in the following format: crontab-l |-r |-e |-I [username]. The parameter meanings are shown in Table 1: parameter name meanings example-l displays the contents of the Crontab file. Before you delete the Crontab file of a user, you are prompted to delete the Crontab file crontabl-ri-r-e from the Crontab directory to edit the Crontab file crontabl-e. the Crontab file created by the user is saved in/var/spool/cron, the file name is the same as the user name. The format is divided into six segments. The first five segments are time segments, and the sixth segment is the command segment to be executed. The format is as follows: ***** the meanings of the time periods are shown in table 2: the value range is minute 0-59. The second segment represents hour 0-23. The third segment represents date 1-31. The fourth segment represents Month 1-12. The fifth segment represents the day of the week. 0 represents Sunday 0-6: if the content of your Crontab file is: 29 19 *** echo its dinner time, the system will display the 'its dinner time' example at every day (create a cron (, and input the current time in test.txt every minute): 1. log on to linux as a common user (I use CentOS4.1) 2. $ crontab-e: the Default Editor of the system is VIM. If not, add the following shell: $ EDITOR = vi $ export EDITOR3. enter "*/1 ***** date> $ HOME/test.txt", s Ave and exit VIM4. $ su root5. $ cd/etc/init. d6 .. /crond restart: ● 0 */2 ***/sbin/service httpd restart means to restart apache every two hours ● 50 7 ***/sbin/service sshd start means to enable ssh at every day service ● 50 22 ***/sbin/service sshd stop means to close the ssh service at every day ● 0 0 ** fsck/home checks/home disks on the 1st and 15th of every month ● 1 ***/home/bruce/backup execute the file at the first point of every hour/home/bruce/backup ●00 03 ** 1-5 find/home "*. xxx "-mtime + 4-e Xec rm {}/; find the file named *. xxx in the/home directory from Monday to Friday three o'clock, and delete the file four days ago. ● 30 6 */10 ** ls means to execute the ls command parameter at on the first, 11th, 21st, and 31st of each month: crontab-e: run the text editor to set the time table. The preset text editor is VI. If you want to use another text editor, first, set the VISUAL environment variable to specify the Text Editor (for example, setenv VISUAL joe) crontab-r: Delete the current time table crontab-l: list the current time table crontab file [-u user]-replace the current crontab with the specified file. The time table format is as follows: f1 f2 f3 f4 f5 program where f1 represents minutes, f2 represents hours, f3 represents the day of a month, and f4 represents the month, f5 indicates the day of the week. Program indicates the program to be executed. When f1 is *, the program is executed every minute. If f2 is *, the program is executed every hour, and so on. When f1 is a-B, it indicates that it will be executed from minute a to minute B, when f2 is a-B, it indicates that execution is performed from hour a to hour B, and so on. When f1 is */n, it indicates that execution is performed every n minutes, if f2 is */n, the task is executed every n hours. Similarly, when f1 is a, B, c ,... a, B, c ,... execute in minutes. f2 is a, B, c ,... a, B, c... you can save all settings in the file and use crontab file to set the time table. Crontab: crontab-l view the crontab configuration of the user, crontab-e edit the crontab configuration of this user. Generally, 2> & 1 is added at the end of the configuration, indicating the error output (2) and standard output (1) output to the same place specified by>, for example, 15 14 */sys_back/monitor. sh>/sys_back/log/monitor. log 2> & 1 indicates that the monitor is executed at every day. sh script, errors and standard output are written to monitor. when the log file involves the file name, it is best to write absolute path 2 and solve the problem that shell scripts have echo statements and java-jar to execute java programs. When the script is executed directly, everything goes smoothly, but when it is executed in crontab, the echo statement is normal, but the java program is not executed. 1. I think it should be an environment variable. Add the java environment variable to the shell script. Follow this method to find that the problem persists. Think twice about it. 2. From the perspective of conversion, check whether the java program is faulty. Add the print statement directly to the java statement and find that it can be displayed in the crontab log. Finally, it is a java program problem. Java program functions are very simple, that is, a statement java-Dosgi. console-Dosgi. configuration. area =./configuration-jar equinox. jar-console is used to start the osgi framework. Therefore, if you do not call the java program in the shell script, you can directly change it to the one in the program to solve the problem. However, a new problem arises: Direct java-jar will continuously output osgi> to log files, resulting in larger and larger log files. Not feasible. Try again. 3. The problem is still relative path. An attempt is made to output a sentence to a file using a relative path in the script called by crontab, And a failure is found. (It's Okay To execute the script separately.) is it true that relative paths cannot be used in java programs. The./configuration and equinox. jar files are all in the absolute path and then debugged. The problem is finally solved. However, it is always inconvenient to maintain the absolute path in the program. 3. Additional issues and the purpose of solving this script is to determine whether the osgi program is running. If it is not running, it is started. The implementation idea is to run ps-elf to get the process id and status. If the id does not exist, it is started. If the id exists but the status is not running (solaris is O, AIX is ), then the original process is killed and restarted. It is found that the program will automatically stop after running for a few days, thinking that there is a problem with the java program, but it is always unable to find the problem. When solving the above problem, we accidentally found that it may not be a java program problem. In solaris, apart from O and S (sleeping) R (Runnable) Z (Zombie process) T (stopped ), only the process in the last two States is faulty. Therefore, it is likely that when the crontab executes the script, the program is killed if it is not in the O state, but fails to be restarted due to the above problem. Therefore, the script is restarted only when the status is the last two. This problem is also solved. Coconut_zhang

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.