且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

确保Linux中应用程序的单个实例

更新时间:2023-11-22 20:43:28

有几种常见的技术,包括使用信号量.我看到的最常用的文件是在启动时创建一个"pid锁定文件",其中包含正在运行的进程的pid.如果程序启动时文件已经存在,请打开文件并获取其中的pid,检查是否正在运行具有该pid的进程,如果已检查/proc/ pid 中的cmdline值>查看它是否是程序的实例,如果已退出,则用pid覆盖文件. pid文件的常用名称是 application_name .pid.

There are several common techniques including using semaphores. The one I see used most often is to create a "pid lock file" on startup that contains the pid of the running process. If the file already exists when the program starts up, open it up and grab the pid inside, check to see if a process with that pid is running, if it is check the cmdline value in /proc/pid to see if it is an instance of your program, if it is then quit, otherwise overwrite the file with your pid. The usual name for the pid file is application_name.pid.