Capture Process id from Our application name

If it doesn't fit in another category, ask it here.
Post Reply
karthikreddysid
Posts: 1
Joined: 2011/05/02 08:03:40

Capture Process id from Our application name

Post by karthikreddysid » 2011/05/02 08:32:58

Anyone can help me out from this issue, great thanks in advance.
When i am executing following shellscript, i could not able to get correct process id . Every time i am getting three lesser to actual process id as shown below.
[code]
prog="monitorgateway"
. /etc/init.d/functions
case "$1" in
start)
sudo nohup /opt/cc/bin/start.sh
ps -eo pid,tid,wchan:14,command | grep ProxyDaemonLauncher | cut -f 2 -d ' ' | head -n 1 > /var/monitorgateway.pid
;;
stop)
pid=$(cat /var/monitorgateway.pid)
sudo kill -9 $pid
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[/code][Moderator edit: Added [i]code[/i] tags to preserve formatting.]

Output: Process id in the monitorgateway.pid is 5612, but actual process id is 5615

When i am executing following command directly on terminal, i can get correct process id as shown below
ps -eo pid,tid,wchan:14,command | grep ProxyDaemonLauncher | cut -f 2 -d ' ' | head -n 1 > /var/monitorgateway.pid
Process id in the monitorgateway.pid is 5615, actual process id also 5615

User avatar
jlehtone
Posts: 2931
Joined: 2007/12/11 08:17:33
Location: Finland

Capture Process id from Our application name

Post by jlehtone » 2011/05/06 12:26:39

ps|grep|cut|head???

Look at the /etc/init.d/*
Those scripts do use pid.

There is even '/sbin/pidof'

Or your /opt/cc/bin/start.sh could create a pid-file. It surely knows its own pid.
$$, like for example /etc/cron.daily/mlocate.cron shows.

User avatar
TrevorH
Forum Moderator
Posts: 29112
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Capture Process id from Our application name

Post by TrevorH » 2011/05/06 14:03:28

I suspect that whatever is being launched is forking itself as a daemon and the script is not catching this. Not quite sure how you'd handle that, sleeping in the script seems like a bit of a bodge :)

Post Reply

Return to “CentOS 5 - Miscellaneous Questions”