linux deamon中常使用的pid file
来源:互联网 发布:穆赫兰道 知乎 编辑:程序博客网 时间:2024/06/14 22:56
我们可以用下面的一个脚本来自动在校本启动的时候建立自己的pid file,并在退出的时候删除pid file:
不过,要注意的是,下面的查找pid的方法,其实比脚笨,用$$不就可以了么,只不过,这个脚本命令的意义在于如果在别的脚本中,可以查看另一个脚本的pid。因此这个方法作为另一个参考。
#!/bin/bash
//取得当前脚本名
EXE_WITH_PATH=$0
//将脚本名前可能带有的路径去掉
EXE_WITHOUT_PATH=${EXE_WITH_PATH#*/}
//定义pid fie
PID_FILE=/var/run/${EXE_WITHOUT_PATH}.pid
//输出所有的process信息,查找本脚本的process, 找出第一行, 针对一行数据,以空格作为delimiter,取第6个field,就是pid信息,将其写到pid file里面。
ps -ef | grep "${EXE_WITHOUT_PATH}" | head -n1 | cut -d" " -f6 >${PID_FILE}
。。。。具体的一些别的操作
if [ -e ${PID_FILE} ]; then
rm ${PID_FILE}
fi
此外,还有个pidof命令可以用来查看一个executable文件的进程的pid,当然,如果这个executable有多个进程在运行,那么我们得到的就是多个pid.
当然,我们还看到过lock file,一般都放在/var/lock/subsys/目录下面,如果一个deamon已经启动了,那么它的lock文件就应该存在了。
下面是一片对pid file的介绍:
What is a Pid-File?
A Pid-File is a file containing the process identification number (pid) that is stored in a well-defined location of the filesystem thus allowing other programs to find out the pid of a running script.
Daemons needs the pid of the scripts that are currently running in the background to send them so called signals. Daemons uses theTERM signal to tell the script to exit when you issue a stop command.
How does a Pid-File look like?
Pid-Files generated by Daemons have to following format:
<scriptname>.rb<number>.pid
(Note that <number> is omitted if only one instance of the script can run at any time)
Each file just contains one line with the pid as string (for example 6432).
Where are the Pid-Files stored?
Daemons is configurable to store the Pid-Files relative to three different locations:
- in a directory relative to the directory where the script (the one that is supposed to run as a daemon) resides (:script option for:dir_mode)
- in a directory given by :dir (:normal option for :dir_mode)
- in the preconfigured directory /var/run (:system option for :dir_mode)
- linux deamon中常使用的pid file
- linux使用pid file
- linux下的deamon进程
- linux下的deamon进程
- linux下的deamon进程
- linux下的deamon进程
- pid file in linux
- Linux Deamon编程方法
- Linux Deamon编程方法
- Linux Deamon编程方法
- Linux Deamon编程方法
- Linux Deamon编程方法
- linux服务deamon
- linux/Android LED deamon
- linux安装deamon
- Linux C/C++ 找不到deamon函数声明的解决方法
- 编码中常使用的工具
- iOS中常使用的开源类库
- java构造函数
- Android界面基本属性
- Linux 下的终端文件管理mc
- Drools项目发布成Web服务
- C语言---数据类型
- linux deamon中常使用的pid file
- KeilC51使用printf输出到串口打印的实现
- 遍历二叉树
- Swift学习笔记(一)——Swift的数据类型
- hashcode
- LeetCode OJ - Rotate Image
- little crawler for clojure
- debugfs命令恢复被误删的文件
- 如果在本地搭一个服务器和mysql数据库环境,如果使用java来访问数据库