Regular tasks of those things

Crond Job Service Overview

What is Crond

What is a scheduled task, plan task is similar to our usual alarm clock life.
In the Scheduled Tasks service crond Linux system to meet the needs periodically perform a task.
crond process will deal with every minute scheduled task, the main task is to plan to do some periodic tasks currently the most important use is timed backup data

Scheduled task and the different types of systems

Timing task windows 7 system

Start → All Programs → Accessories → System Tools → Select Task Scheduler

Linux system work scheduling regular tasks can be divided into the following two situations:

1, the work of the task itself regularly perform Linux system: the system work tasks performed periodically, such as polling system log, backup system data, clearing the system cache, etc.
2, work tasks performed by the user: a user or system administrator to regularly do the tasks of jobs, such as the time server for time synchronization on the Internet every five minutes and, in the evening every day 0:00 back up site data and database data, these generally work needs to be set by each user their own job.

Detailed profiles Crond

/ etc / crontab # Crontab-configuration file
 / etc / the cron.deny # listed in the file the user is not allowed to use crontab command
 / var / spool / cron / *    # timing of all users files are stored in this directory, the file with your name 
/ var / log / the cron * /     log file after the task execution timing of #, can be used to backtrack

Timing task instructions crond

[root @ iflytek- 30 ~] # crontab - Help 
crontab: invalid option - - 
crontab: Usage error: out with unrecognized the Option 
Usage: 
 crontab [Options] File 
 crontab [Options] 
 crontab -n [ hostname ] 

Options:
  -e Edit the User ' S crontab # crontab file to edit the contents of 
 the -l List the User ' S crontab # crontab file to view the contents of 
 -r the delete the User ' S crontab # crontab delete the contents of the file 
Note: crontab -l and -e is actually in operation / var / spool / cron / username

crond time meaning

[root@iflytek-30 ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)  #分钟
# | .------------- hour (0 - 23)    #小时
# | | .---------- day of month (1 - 31)   #日期
# | | | .------- month (1- 12 ) OR jan, feb, Mar, apr month ... # 
# | | | | .---- Day of Week ( 0 - 6 ) (Sunday = 0 or 7 ) OR Sun, Mon, tue, Wed, thu, fri, sat # week (0 and 7 represent Sunday) 
# | | | | | 
# * * * * * user- name to BE the Executed the Command 

# * indicates any time, is "every" means, for example: if 00  01 * * * cmd represent daily 1:00 to perform tasks cmd monthly weekly 
# - representing the separator, represents a period of time, such as 17-19 points per hour of 00 minutes to perform the task. 00  17 - 19 * * * cmd. Is 17, 18 , 19 points, respectively, performed the whole point mean 
#, comma represents the mean division time period. 30  . 17 , 18 ,. 19 * 17 * * cmd indicates a day, 18 slightest performed cmd, 19 points. 
# / Nn represents a number that every n unit of time, such as columns: every ten minutes to perform a task may write * / 10 * * * * cmd

Writing Crond timer task seven basic essentials

1, plus the necessary rules for the regular tasks comment

#time sync 
* * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2>&1

2, scheduled tasks command or program is best written script execution

#backup www to /backup
00 00 * * * /bin/sh /server/scripts/www_bak.sh >/dev/null 2>&1

3, the script performs the task to regulate the timing of the path, such as: a / the server / scripts, reference 

4, when executing a shell script task before plus / bin / sh, the last on the reference 

5, the timing task ends plus> / dev / null 2> & 1, with reference to the previous upper 

Note: If the timing task ends without specification> / dev / null 2> & 1, can easily lead to hard disk inode space is filled, so that the system is not normal service. 

6, performed in the specified user task timed: This environment variable pay special attention to different users, if the system environment variables are called / etc / profile, is best used in the script environment variables under re-export, can be defined directly. / etc / profile in the script 

7, program production tasks not arbitrarily printout information: After commissioning a good script, content information and should try to DEBUG command output masked, if you really need the output log, can be directed to a log file, to avoid the system garbage.

Crond configured to write a real column

# Chi every morning cutting nginx date
 00  00 * * * / bin / SH / Soft / scripts / cut_nginx. SH &> / Soft / scripts / log / nginx.log 
# every day 5:00 back up the database 
00  05 * * * / bin / SH / Soft / scripts / dump_sql. SH &> / Soft / scripts / log / mysql.log 
# Test database every 5 minutes is normal
 * / . 5 * * * * / bin / SH / Soft / scripts / start_mysql. SH &> / dev / null 

# Note: 
# 1 . All our crond service is running. The user crontab command is a command to set the timing rule 
# 2 .crond service is commonly used in the production of important work service 
# 3 . Almost every server will be used crond service

Crond program debugging tasks

1. Every minute adjustment task, whether testing is normal, not frequently perform some tasks
2. Adjust the system time and then in the detection task, the production does not recommend using this method directly
3. The execution of the script, the script execution input into the specified log file, whether the contents of the log normal observation
4. attention to the problems caused by some of the tasks command echo "xujun" >> / tmp / xujun.log &> / dev / null
5. command using an absolute path, the command can not be found to prevent the scheduled task execution results in failure
6 View / var / log / cron logs for debugging
suggestions: the need to periodically perform tasks written script, build / server / scripts directory unified storage script, the script command must be an absolute path, manually execute the script detection output is normal, the script then join a scheduled task to test, test script output no problem to write the corresponding log file can be.

Guess you like

Origin www.cnblogs.com/xujun1270/p/11280781.html