linux deamon中常使用的pid file


   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 Pid-File?

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 signalsDaemons 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:

  1. 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)
  2. in a directory given by :dir (:normal option for :dir_mode)
  3. in the preconfigured directory /var/run (:system option for :dir_mode)
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值