You are not logged in.

#1 2010-01-11 21:01:19

jb234
Member
From: Norway
Registered: 2009-06-22
Posts: 25
Website

get pid in rc.d script

Hi,

I am trying to write a rc.d script. The script works fine, except I am not sure how to get the pid into a file.

The problem is in the "start" case

echo $? > $PIDFILE

I am not sure if this should work, it would return the pid of the sudo process right?
It starts ok, but it always writes '0' into the pid file.

I have looked at some other rc.d scripts, such as http://wiki.archlinux.org/index.php/Wri … .d_scripts
but they seem to use the process name and I think that that won't work since there will be many with the same name.

#!/bin/bash

PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/java/jdk1.6.0_10/bin:/usr/local/ant/apache-ant-1.7.1/bin
DESC="node 1"
NAME=node1
DAEMON=node1
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid

. /etc/rc.conf
. /etc/rc.d/functions

case "$1" in
  start)
    stat_busy "Starting..."
    sudo -u jberg ant -f /home/jberg/node/build.xml run > /home/jberg/node/node1.log &
    if [ $? -gt 0 ]; then
      stat_fail
     else
      add_daemon $NAME
      echo $? > $PIDFILE
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping node 1..."
    if [ -a $PIDFILE ]; then
      PID='cat $PIDFILE'
      kill $PID > /dev/null
      if [ $? -gt 0 ]; then
        stat_fail
      else
        rm $PIDFILE
        rm_daemon $NAME
        stat_done
      fi
    else
      pkill $NAME
      if [ $? -gt 0 ]; then
        stat_fail
      else
        stat_done
      fi
    fi
    ;;
  restart|force-reload)
    $0 stop
    sleep 1
    $0 start
    ;;
esac
exit 0

Last edited by jb234 (2010-01-11 21:02:24)

Offline

#2 2010-01-11 21:21:48

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: get pid in rc.d script

"echo $?" will give you the status of the just exited process (man bash)
However, "echo $$" will give you the pid ... if you are in a subshell, it will give the pid of the 'controlling' shell

Last edited by perbh (2010-01-11 21:24:34)

Offline

#3 2010-01-11 23:45:54

jb234
Member
From: Norway
Registered: 2009-06-22
Posts: 25
Website

Re: get pid in rc.d script

Thanks for your reply.
You were correct and I got one step further.

Now the problem is that the PID that comes out after I run ant:

ant -f /home/jberg/node/build.xml run > /home/jberg/node/node1.log &
if [ $? -gt 0 ]; then
      stat_fail
     else
      add_daemon $NAME
      echo $$ > $PIDFILE
      stat_done
    fi
    ;;

is not the pid of any running process. It is a few numbers lower than the pid of the VM and if I try to kill it it says no such process.
So I suspect it is the way ant starts that forks off a process in some way.

I could try to get the pid from inside java and write it to a file, but I suspect that will also be a bit painful.

Any more ideas?

Last edited by jb234 (2010-01-11 23:48:41)

Offline

#4 2010-01-12 00:04:17

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: get pid in rc.d script

I think $! will give you the pid of the most recently backgrounded process. $$ is the pid of your own process. If you want to do something even fancier, you'll need to use ps or pgrep or something like that.

Offline

#5 2010-01-12 00:50:56

jb234
Member
From: Norway
Registered: 2009-06-22
Posts: 25
Website

Re: get pid in rc.d script

I used $!

Cool, I think it is working.

Offline

Board footer

Powered by FluxBB