[arch-general] get pid of daemon in init script
Hi to .* I must write a rc init script for a server I am packaging. Therefor I have copied the init script of cups. Now I recognized that both pid files, cups.pid and my own, in /var/run are empty. Further investigations have shown that "pidof -o %PPID -x /usr/bin/cdvserver" returns nothing in the start) case but in stop) it seems to work. I don't understand why pidof isn't working as expected. Kind Regards, Michael Krauss Here is my init script: #!/bin/bash . /etc/rc.conf . /etc/rc.d/functions PID=`pidof -o %PPID -x /usr/bin/cdvserver` PIDFILE="/var/run/cdvserver.pid" case "$1" in start) stat_busy "Starting Codeville Server" [ -z "$PID" ] && /usr/bin/cdvserver if [ $? -gt 0 ]; then stat_fail else echo "Started " $PID echo $PID > $PIDFILE add_daemon cdv stat_done fi ;; stop) stat_busy "Stopping Codeville Server" [ ! -z "$PID" ] && kill $PID &> /dev/null if [ $? -gt 0 ]; then stat_fail else rm $PIDFILE rm_daemon cdv stat_done fi ;; restart) $0 stop sleep 1 $0 start ;; *) echo "usage: $0 {start|stop|restart}" esac exit 0
On Feb 8, 2008 1:27 PM, Michael Krauss <hippodriver@gmx.net> wrote:
Hi to .*
I must write a rc init script for a server I am packaging. Therefor I have copied the init script of cups. Now I recognized that both pid files, cups.pid and my own, in /var/run are empty.
Further investigations have shown that
"pidof -o %PPID -x /usr/bin/cdvserver"
returns nothing in the start) case but in stop) it seems to work. I don't understand why pidof isn't working as expected.
Because you are calling pidof before the process starts- bash does not do lazy evaluation of your expressions. If you want to store the PID of the newly started process, you will have to make another PID=... call to get it in the else block where you access it.
Here is my init script: #!/bin/bash
. /etc/rc.conf . /etc/rc.d/functions
PID=`pidof -o %PPID -x /usr/bin/cdvserver` PIDFILE="/var/run/cdvserver.pid" case "$1" in start) stat_busy "Starting Codeville Server" [ -z "$PID" ] && /usr/bin/cdvserver if [ $? -gt 0 ]; then stat_fail else echo "Started " $PID echo $PID > $PIDFILE add_daemon cdv stat_done fi ;; stop) stat_busy "Stopping Codeville Server" [ ! -z "$PID" ] && kill $PID &> /dev/null if [ $? -gt 0 ]; then stat_fail else rm $PIDFILE rm_daemon cdv stat_done fi ;; restart) $0 stop sleep 1 $0 start ;; *) echo "usage: $0 {start|stop|restart}" esac exit 0
Hi! It works perfectly (at least for me), just you have to ensure, that you call it with appropriate rights while testing: my testing: sentinel@sentinel:~$ su Password: with power comes great responsibility root@sentinel:/home/sentinel# PID=`pidof -o %PPID -x /usr/sbin/proftpd` root@sentinel:/home/sentinel# echo $PID 7469 root@sentinel:/home/sentinel# [ -z "$PID" ] && echo "pid is empty" root@sentinel:/home/sentinel# Tom Dan McGee wrote:
On Feb 8, 2008 1:27 PM, Michael Krauss <hippodriver@gmx.net> wrote:
Hi to .*
I must write a rc init script for a server I am packaging. Therefor I have copied the init script of cups. Now I recognized that both pid files, cups.pid and my own, in /var/run are empty.
Further investigations have shown that
"pidof -o %PPID -x /usr/bin/cdvserver"
returns nothing in the start) case but in stop) it seems to work. I don't understand why pidof isn't working as expected.
Because you are calling pidof before the process starts- bash does not do lazy evaluation of your expressions.
If you want to store the PID of the newly started process, you will have to make another PID=... call to get it in the else block where you access it.
Here is my init script: #!/bin/bash
. /etc/rc.conf . /etc/rc.d/functions
PID=`pidof -o %PPID -x /usr/bin/cdvserver` PIDFILE="/var/run/cdvserver.pid" case "$1" in start) stat_busy "Starting Codeville Server" [ -z "$PID" ] && /usr/bin/cdvserver if [ $? -gt 0 ]; then stat_fail else echo "Started " $PID echo $PID > $PIDFILE add_daemon cdv stat_done fi ;; stop) stat_busy "Stopping Codeville Server" [ ! -z "$PID" ] && kill $PID &> /dev/null if [ $? -gt 0 ]; then stat_fail else rm $PIDFILE rm_daemon cdv stat_done fi ;; restart) $0 stop sleep 1 $0 start ;; *) echo "usage: $0 {start|stop|restart}" esac exit 0
On Feb 8, 2008 3:06 PM, Sentinel <kanocz@intrak.sk> wrote:
Hi!
It works perfectly (at least for me), just you have to ensure, that you call it with appropriate rights while testing: my testing: sentinel@sentinel:~$ su Password: with power comes great responsibility root@sentinel:/home/sentinel# PID=`pidof -o %PPID -x /usr/sbin/proftpd` root@sentinel:/home/sentinel# echo $PID 7469 root@sentinel:/home/sentinel# [ -z "$PID" ] && echo "pid is empty" root@sentinel:/home/sentinel#
Tom
That is because you already have /usr/sbin/proftpd running. In the case of running the start initscript, the PID is sampled before the application ever starts, and hence will return no PID, as Dan already explained.
yes you are right. root@sentinel:/home/sentinel# /etc/rc.d/proftpd stop :: Stopping ProFTPd Server [DONE] root@sentinel:/home/sentinel# PID=`pidof -o %PPID -x /usr/sbin/proftpd` root@sentinel:/home/sentinel# echo $PID root@sentinel:/home/sentinel# Sorry, I made a mistake. I usually do not have my ftp server running. But I forgot, that I was moving something between my computer and laptop. :) Btw proftpd's init script does the same. The proftpd.pid file is empty. T Travis Willard wrote:
On Feb 8, 2008 3:06 PM, Sentinel <kanocz@intrak.sk> wrote:
Hi!
It works perfectly (at least for me), just you have to ensure, that you call it with appropriate rights while testing: my testing: sentinel@sentinel:~$ su Password: with power comes great responsibility root@sentinel:/home/sentinel# PID=`pidof -o %PPID -x /usr/sbin/proftpd` root@sentinel:/home/sentinel# echo $PID 7469 root@sentinel:/home/sentinel# [ -z "$PID" ] && echo "pid is empty" root@sentinel:/home/sentinel#
Tom
That is because you already have /usr/sbin/proftpd running. In the case of running the start initscript, the PID is sampled before the application ever starts, and hence will return no PID, as Dan already explained.
participants (4)
-
Dan McGee
-
Michael Krauss
-
Sentinel
-
Travis Willard