pidfile 与 logfile的不同

发布时间:2020-09-20 17:03:42 作者:落寞三少
阅读:1203
开发者专用服务器限时活动,0元免费领! 查看>>

原文答案:http://unix.stackexchange.com/questions/12815/what-are-pid-and-lock-files-for

大意翻译如下:

pidfile 是用户程序正在开始运行期间,将进程ID写进pidfile文件,主要目的有三个:

    1.为其他程序或操作系统标识本程序正在运行,或至少启动成功了。

    2.如果计划要用kill命令终止进行,它允许一个可写脚本非常容易地检测程序运行状态。

    3.有pidfile后,可以用非常少的代价去获取前一个运行实例因何没有退出成功

仅仅存在pidfile并不能保证进程id是否正在运行,这个方法不是100%简单,但对大多数程序来说是足够好的,检测进程id是否在进程表这个方式并不能跨平台使用,除非使用 ps工具。

lockfile 通常用来保证同一程序的两个实例不能同时运行,使用完后别忘记删除这个文件

pid files are written by some programs to record their process ID while they are starting. This has multiple purposes:

Mere presence of a pid file doesn't guarantee that that particular process id is running, of course, so this method isn't 100% foolproof but "good enough" in a lot of instances. Checking if a particular PID exists in the process table isn't totally portable across UNIX-like operating systems unless you want to depend on the ps utility, which may not be desirable to call in all instances (and I believe some UNIX-like operating systems implement ps differently anyway).

Lock files are used by programs to ensure two (well-behaved) separate instances of a program, which may be running concurrently on one system, don't access something else at the same time. The idea is before the program accesses its resource, it checks for presence of a lock file, and if the lock file exists, either error out or wait for it to go away. When it doesn't exist, the program wanting to "acquire" the resource creates the file, and then other instances that might come across later will wait for this process to be done with it. Of course, this assumes the program "acquiring" the lock does in fact release it and doesn't forget to delete the lock file.

This works because the filesystem under all UNIX-like operating systems enforces serialization, which means only one change to the filesystem actually happens at any given time. Sort of like locks with databases and such.

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. 基于python求两个列表的并集.交集.差集
  2. python怎么实现串口通信

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pidfile lockfile og

上一篇:对django中foreignkey的简单使用详解

下一篇:Android开发实现自定义Toast、LayoutInflater使用其他布局示例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×