1827459 Members
3693 Online
109965 Solutions
New Discussion

getpid

 
SOLVED
Go to solution
Tom Satinet
Frequent Advisor

getpid

Hello,

I've created a script that monitors a process that runs in on our sever. I have also created a script in /etc/rc.config.d/ and /sbin/init.d to start this script at boot time. however i want to create a file that contains the PID of this script. From within the script i want to divert the process id to a file so my init script knows which process to kill. I know getpid does this but i am not sure how to use it.... can someone help?


also is this alright? do i need to background it??


PATH=/sbin:/usr/sbin:/usr/bin
export PATH

if [ -r /etc/rc.config.d/ecommalert ]
then
. /etc/rc.config.d/ecommalert
fi

case "$1" in

"start") if [ "$ECOMMSTART" -ne 0 ]
then
/ops/scripts/ecommalert&
exit 0
fi
exit 2 ;;

"stop") if [ "$ECOMMSTART" -ne 0 ]
then
/ops/scripts/ecommstop
exit 0
fi
exit 2 ;;
*)
echo "usage: $0 {start|stop}"
exit 1
;;
esac

exit 0;
21 REPLIES 21
Arunvijai_4
Honored Contributor

Re: getpid

Hi Tom,

You can take a look at, /sbin/init.d/template
to write your own startup script.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Tom Satinet
Frequent Advisor

Re: getpid

thanks, but i'm more concerned with get the PID of the script. thanks:-)
Arunvijai_4
Honored Contributor

Re: getpid

Hello,

For PID, you can take a look at Secure shell's startup script /sbin/init.d/secsh. It should help you.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Tom Satinet
Frequent Advisor

Re: getpid

not really.

that the sshd writes the PID to a file. this script just cats it and assigns that to a variable.

Tom Satinet
Frequent Advisor

Re: getpid

sorry, maybe i was not clear.

from within the script (not the init.d or rc.config.d) that actually monitors the process. Say its called 'ecommalert'. I want 'ecommalert' to get its own PID and send that to a file.

ecommalert will be started at run level 3.
Arunvijai_4
Honored Contributor

Re: getpid

Hello,

You can probably use something like this,

echo "ecomalert PID is $$" >ecom.pid

-Arun
P.S Remember to assign points.
"A ship in the harbor is safe, but that is not what ships are built for"
paolo barila
Valued Contributor

Re: getpid

Hi,

You can't use getpid() from a shell script,
it should be the process (written in a language like C) that call that function and writes its PID in a file.

From shell you can only do somenthing like:

# ps -efx | grep

Pablo
share share share
Steven E. Protter
Exalted Contributor

Re: getpid

Shalom,

You may find some Linux code useful in this regard.

/usr/local/cluster/cluster.mon &
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/cluster.mon
return $RETVAL
}

thats from a clsuter monitor script. It creates a lock file by PID.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: getpid

Hi Tom:

The following shell variables are directly available to you:

?? -> the process number of the current shell

?! -> the process number of the last background command

Does this help?

Regards!

...JRF...
paolo barila
Valued Contributor

Re: getpid

A simple C program that use getpid()

#include
main()
{
printf("Salve mondo!\n");
printf("PID= %d\n", getpid());
}

share share share
Andy Torres
Trusted Contributor

Re: getpid

Not sure if this will help, but by using the basename of the script you can get the PID and pass it to a file, like in this example, used to get the PID of the scopeux process:

UNIX95= ps -C scopeux -o pid= > scopeux.pid
Tor-Arne Nostdal
Trusted Contributor
Solution

Re: getpid

Hi Tom.
Do you want to stop the ecommalert or the process this script is monitoring (or both)?

If ecommalert is a script, you could simply add a line which echo the PID to a file during startup.

example:
echo "$$" >/ops/scripts/ecommalert.pid

As others have mentioned it is not that simple if the ecommalert is a binary.

Another approach could be to use a killproc function.
You could have a look in /sbin/init.d/nfs.client to see how it is used, and how to define the function.

This use a unique string input on the process you want to kill, rather than the PID itself.

Example
You would be writing:
killproc ecommalert
Instead of writing:
kill `cat /ops/scripts/ecommalert.pid`

The killproc function will use ps to find and grep the correct process, then kill it.

======= some hint/tips =======
If you use a PID-file you can check if the process is alive with this command:
#sh-posix syntax
kill -0 `cat /ops/scripts/ecommalert.pid` || {
print "Warning: No such process"
rm -f /ops/scripts/ecommalert.pid
}

In the /sbin/init.d/nfs.client you should notice these three functions.
killproc : "nice kill"
killbiod : identical, but "sure kill" (-9)
findproc : returning the PID for a named process

To test this I suggest you copy the functions into a new script which you can do some testing with.
Then change the kill $pid to echo "kill $pid"
Then you can safely try it out and see that the correct PID's are used.

Last hint:
If you have many functions which is useful for many purposes you can define these in separate files.
example:
/ops/scripts/Functions/killproc
/ops/scripts/Functions/findproc
containing the function(s) from nfs.client
When you want to use functions in /ops/scripts/Functions you simply set the environment variable FPATH
(export FPATH=/ops/scripts/Functions)
- and then they're ready for use.
can be used both in scripts as well as from command-line.

To see functions defined: typeset -f

Best regards
Tor-Arne
I'm trying to become President of the state I'm in...
Tom Satinet
Frequent Advisor

Re: getpid

Hey thanks! :-)

this is very useful to me.

I am just using a shell script now, before i learn C. but i am very interesting in process control.

I'll go away and do some testing :-)

Thanks for your time.
Geoff Wild
Honored Contributor

Re: getpid

UNIX95 is a good way to go:

MYPID="$(UNIX95= ps -C $MYPROC -o pid=)"

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Tom Satinet
Frequent Advisor

Re: getpid

yes i'm using that, it's cool
Tor-Arne Nostdal
Trusted Contributor

Re: getpid

When something is useful we appreaciate assigned points ;-))
I'm trying to become President of the state I'm in...
Tom Satinet
Frequent Advisor

Re: getpid

as you will see i have assigned 100% of points so far. impatience eh! ok ok......
Tor-Arne Nostdal
Trusted Contributor

Re: getpid

he, he...
The points are encouraging activity, though most sysadmins is willing to help out and share knowledge even if it doesn't "pay off". It's almost as built into the spinal cord :-))
I'm trying to become President of the state I'm in...
Tom Satinet
Frequent Advisor

Re: getpid

one question i have - if i run a script from an init script, do i need to background it (&), or can i just run it?

bearing in mind the script runs for ever...
Arunvijai_4
Honored Contributor

Re: getpid

Hello, You asked about "one question i have - if i run a script from an init script, do i need to background it (&), or can i just run it?

bearing in mind the script runs for ever..."

No need to put it into background process. Init will take care of it.

http://enterprise.linux.com/article.pl?sid=05/08/02/1821218&tid=129

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Tom Satinet
Frequent Advisor

Re: getpid

thanks i presumed that was the case. just wanted to make 100% sure.