linux中一些deamon程序,由于一般只运行一个,因此就只有一个唯一的pid,因此为了其他程序方便知道该deamon的pid,就自建了一个pid file,一般是/var/run/deamon_name.pid,比如syslogd就是/var/run/syslogd.pid. 这个文件的内容就是这个deamon的pid.
我们可以用下面的一个脚本来自动在校本启动的时候建立自己的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 -File?
A -File is a file containing the
process identification number () that is stored in a well-defined
location of the filesystem thus allowing other programs to find out the of a running script.
needs the of the scripts that are currently
running in the background to send them so called signals. uses the TERM signal to tell
the script to exit when you issue a stop command.
How does a -File look like?
-Files generated by have to following format:
.rb.pid
(Note that is omitted if only one instance of the
script can run at any time)
Each file just contains one line with the as string (for example 6432).
Where are the -Files stored?
is configurable to store the -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)
阅读(6229) | 评论(0) | 转发(1) |