In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to ensure the uniqueness of the application in the script". The content is simple and clear, hoping to help you solve your doubts. Let the editor lead you to study and learn the article "how to ensure the uniqueness of the application in the script".
A solution to shell script
Creating an existing file directory using the features of mkdir will fail. The first time the program runs, you can create a / tmp/lock folder that indicates that a program is currently running, and when you start the second program, mkdir / tmp/lock will fail.
#! / bin/bash
Mkdir / tmp/lock
If [$?-ne 0]; then
Echo "there is tr script running..."
Exit 1
Fi
Trap "rm-fr / tmp/lock" SIGINT SIGTERM
Sleep 50
If [- d / tmp/lock]; then
Rm-fr / tmp/lock
Echo "rm-fr / tmp/lock"
Fi
Note that the trap command in linux prevents the script from terminating abnormally: by kill (not kill-9), crtl+c interrupts more detailed information about "trap of the Linux command-processing signals in the script" http://codingstandards.iteye.com/blog/836588
Second, the solution of python script
When you search the Internet for python locked files, you will be prompted by the fcntl module. Python's file locks currently use the fcntl library, which actually provides an interface for ioctl,flock and fcntl functions on Unix.
Fcntl module function flock (file_handle, operation)
Where file_handle represents a file descriptor and operation refers to the lock operation to be performed, there are several types:
Fcntl.LOCK_UN unlock: delete the lock created by the floc () function
Fcntl.LOCK_EX exclusive lock: processes other than locked processes do not have read and write access to locked files.
Fcntl.LOCK_SH shared locks: all processes do not have write access, even locked processes. All processes have read access.
Fcntl.LOCK_NB non-blocking lock: this parameter means that the function will return immediately if it cannot acquire the file lock, otherwise, if the lock is not successfully requested using LOCK_EX/LOCK_SH, the current process will wait for the file lock to be obtained. Using LOCK_NB can not block the process while acquiring this exclusive lock, and LOCK_NB can also perform bitwise or (|) operations with LOCK_SH or LOCK_NB, such as fcnt.flock (file_handle,fcntl.LOCK_EX | fcntl.LOCK_NB), so that the system will not block the current process.
Note:
1. The f.close () operation on the file will invalidate the file lock
two。 The file lock fails after the end of the main process
3. The LOCK_EX of flock () is an "advice lock". The kernel does not force the state of the lock to be checked, but needs to be explicitly checked where the file is manipulated in the code to take effect.
Test script
In the script, the is_running function is used to lock the file, and time.sleep (10) simulates the program that has been executed for a long time, successfully locking the lock.py for the first time, and running lock.py again during the run of the program, which will fail to acquire the lock and exit the program in time.
#! / usr/bin/python2.6
# coding:utf8
Import time
Import fcntl
Import sys
Def is_running (file):
Lock_file=open (file, "w")
Try:
Fcntl.lockf (lock_file,fcntl.LOCK_EX | fcntl.LOCK_NB)
Print "lock the file, please wait for 10s."
Except:
The print 'file is locked and cannot be executed. Please run it later.'
Return None
Return lock_file
If _ name__ = = "_ _ main__":
Lockfile= "/ tmp/rsync_is_running"
A=is_running (lockfile)
If an is None:
Print "lock file failed, rsync is running."
Sys.exit (0)
Else:
Print "lock file successed!!"
Time.sleep (10)
Test examples:
Conversation one
Conversation two
The above is all the content of the article "how to ensure the uniqueness of the application in the script". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 299
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.