Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 09:51 PM
02-06-2006 09:51 PM
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;
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 10:02 PM
02-06-2006 10:02 PM
Re: getpid
You can take a look at, /sbin/init.d/template
to write your own startup script.
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 10:04 PM
02-06-2006 10:04 PM
Re: getpid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 10:07 PM
02-06-2006 10:07 PM
Re: getpid
For PID, you can take a look at Secure shell's startup script /sbin/init.d/secsh. It should help you.
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 10:11 PM
02-06-2006 10:11 PM
Re: getpid
that the sshd writes the PID to a file. this script just cats it and assigns that to a variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 10:16 PM
02-06-2006 10:16 PM
Re: getpid
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 10:21 PM
02-06-2006 10:21 PM
Re: getpid
You can probably use something like this,
echo "ecomalert PID is $$" >ecom.pid
-Arun
P.S Remember to assign points.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 10:22 PM
02-06-2006 10:22 PM
Re: getpid
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 10:30 PM
02-06-2006 10:30 PM
Re: getpid
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 11:20 PM
02-06-2006 11:20 PM
Re: getpid
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 11:27 PM
02-06-2006 11:27 PM
Re: getpid
#include
main()
{
printf("Salve mondo!\n");
printf("PID= %d\n", getpid());
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2006 01:16 AM
02-07-2006 01:16 AM
Re: getpid
UNIX95= ps -C scopeux -o pid= > scopeux.pid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 03:53 AM
02-08-2006 03:53 AM
SolutionDo 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 04:00 AM
02-08-2006 04:00 AM
Re: getpid
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 04:14 AM
02-08-2006 04:14 AM
Re: getpid
MYPID="$(UNIX95= ps -C $MYPROC -o pid=)"
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 04:15 AM
02-08-2006 04:15 AM
Re: getpid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 04:16 AM
02-08-2006 04:16 AM
Re: getpid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 04:17 AM
02-08-2006 04:17 AM
Re: getpid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 04:36 AM
02-08-2006 04:36 AM
Re: getpid
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 :-))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 08:42 PM
02-08-2006 08:42 PM
Re: getpid
bearing in mind the script runs for ever...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 08:52 PM
02-08-2006 08:52 PM
Re: getpid
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2006 09:04 PM
02-08-2006 09:04 PM