如何确保仅运行该应用程序的一个副本? [英] How to ensure only one copy of the application is running?

查看:104
本文介绍了如何确保仅运行该应用程序的一个副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在Linux上防止多个流程实例

Possible Duplicate:
Preventing multiple process instances on Linux

我有一个多线程应用程序,可以将其作为守护进程运行,也可以使用输入参数运行一次.

I have multi-threaded application which can be run as a deamon process or one time with input parameters.

我想确保如果应用程序是作为守护进程运行的,则不应允许用户再次运行它.

I want to ensure that if the application is running as a deamon process then, user should not be allowed to run this again.

在所有人都建议去牛群之后,我尝试了一下并将其放入服务器.我知道有一个奇怪的问题,当服务器反弹时,它们会删除所有文件,包括锁定文件:(.现在如何?

After you all suggested to go for flocks, I tried it and put it in server. I know have weird problem, when the servers are bounced, they delete all the files, including lock file :(. How now ?

推荐答案

最简单的方法是绑定到端口(可以是unix域,在私有"目录中)只有一个进程可以绑定到端口,因此,如果端口已绑定,则该进程正在运行.如果该进程退出,则内核会自动关闭filedescriptor.它确实使您的进程花费了(未使用?)文件描述符.通常,守护进程始终需要一些监听套接字.

The easiest way is to bind to a port (could be unix domain, in a "private" directory) Only one process can bind to a port, so if the port is bound, the process is running. If the process exits, the kernel automatically closes the filedescriptor. It does cost your process a (unused?) filedescriptor. Normally a daemon process would need some listen socket anyway.

这篇关于如何确保仅运行该应用程序的一个副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆