如何prevent一个Linux程序运行超过一次? [英] How to prevent a Linux program from running more than once?

查看:110
本文介绍了如何prevent一个Linux程序运行超过一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是prevent最好的方式,从在给定时间执行不止一次一个Linux程序/守护进程?

What is the best way to prevent a Linux program/daemon from being executed more than once at a given time?

推荐答案

最常用的方法是创建一个PID文件:定义一个位置,该文​​件将去(的/ var内/运行是很常见)。在成功启动,你会写你的PID此文件。在决定是否启动,读取文件和检查,以确保所引用的进程不存在(或如果这样做,这不是你的守护进程的一个实例:在Linux上,你可以看看的/ proc / $ PID / exe文件)。在关闭时,您可能会删除该文件,但它不是绝对必要的。

The most common way is to create a PID file: define a location where the file will go (inside /var/run is common). On successful startup, you'll write your PID to this file. When deciding whether to start up, read the file and check to make sure that the referenced process doesn't exist (or if it does, that it's not an instance of your daemon: on Linux, you can look at /proc/$PID/exe). On shutdown, you may remove the file but it's not strictly necessary.

有脚本来帮助你做到这一点,你会发现启动 - 停止守护是有用的:它可以使用PID文件,甚至只是在全球范围检查是否存在一个可执行文件。它的设计precisely完成这个任务,并写来帮助人们得到它的权利。

There are scripts to help you do this, you may find start-stop-daemon to be useful: it can use PID files or even just check globally for the existence of an executable. It's designed precisely for this task and was written to help people get it right.

这篇关于如何prevent一个Linux程序运行超过一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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