You are not logged in.
Pages: 1
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
"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
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
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
I used $!
Cool, I think it is working.
Offline
Pages: 1