linux use --flock file locking to solve the problem crontab script Repeat

background

  • After the Linux system crontab configure a timed task, the emergence of a situation, if I set the time crontab task is executed once every minute task script, but the script execution time exceeds 1 minute, such as two minutes, then the system It will perform the task. Resulting in two identical task execution. There may be some concurrency issues in this case, in severe cases can lead to a vicious cycle of dirty data / performance bottlenecks occur.

deal with

  • thought
    • The use of Linux in the flock, will perform regular tasks process the script with the file lock.
  • Detailed flock
    • Summary:
      • flock for the entire file of advisory locks. If a process put a lock on a file (inode), so other processes can be aware of. (Advisory locks are not compelled to comply with the process.) Best of all, it's the first argument is the file descriptor, when the file descriptor is closed, the lock is automatically released. When the process is terminated, all file descriptors will be closed.
      • When multiple processes may execute the same script, these processes need to ensure that no other process in operation, in order to avoid repeated. Typically, such a process will use a "lock file", that is, to create a file to tell myself in another process running, if it detects that the file exists is considered to have the same operation process data at work.
    • use
Flock - H 

Usage: 
 Flock [Options] <File | Directory> <the Command> [the Command args] 
 Flock [Options] <File | Directory> -c <the Command> 
 Flock [Options] <File Number The descriptor> 

Options:
 -s, - - shared: obtain a shared lock
 -x, - exclusive: get an exclusive lock
 -u, - UNLOCK: Removes a lock, is usually not needed, script completes automatically discarded lock
 -n, - NONBLOCK: If you do not get the lock immediately, rather than waiting for failure directly
 -w, - timeout: If you do not immediately get the lock, waiting for a specified time
 -o, - use Close: Close file descriptor before running command. For management and control will not lock if the command generates a child process
 -c, - the Command: In the shell runs a separate command
 -h, - Help displays help
 -V, --version: show the version

Examples

  • My script is as follows
*/1 * * * * flock -xn /home/jingguoliang/project/sh/ordersleep.lock -c '/bin/sh /home/jingguoliang/project/sh/orderbiz.sh >/dev/null 2>&1'
*/1 * * * * flock -xn /home/jingguoliang/project/sh/paysleep.lock -c '/bin/sh /home/jingguoliang/project/sh/paybiz.sh >/dev/null 2>&1'
  • Script Interpreter
    • flock -xn /home/jingguoliang/project/sh/paysleep.lock -c
      • The script on the back plus file locking process, the format is: flock parameter lock file address parameters
    • '/bin/sh /home/jingguoliang/project/sh/paybiz.sh >/dev/null 2>&1'
      • Execute scripts address and print log
      • Note: Be sure to put quotation marks, or the script does not execute! ! !

Guess you like

Origin www.cnblogs.com/zuiyue_jing/p/12557549.html