Operating System - HP-UX
1752815 Members
4531 Online
108789 Solutions
New Discussion юеВ

any idea how to check if .pid file abnormally terminated or not?

 
sekar_1
Advisor

any idea how to check if .pid file abnormally terminated or not?

Admins,
good day.
i am writing a script to check on a given directory if .pid file present or not.
and one more issue.
any idea how to check if file terminated abnormally???
i think abnormal termination should write an exist code in some log file. but in which file and what is that error code number?
i searched in forum and find nothing. so creating this. if already a post is there, please write the link.

Thanks,
Sekar
3 REPLIES 3
Jeeshan
Honored Contributor

Re: any idea how to check if .pid file abnormally terminated or not?

you can use lsof or tusc to do such thing.
a warrior never quits
Ralph Grothe
Honored Contributor

Re: any idea how to check if .pid file abnormally terminated or not?

Hi Sekar,

I would consider as normal program termination (of the process which created the pid file) if the process removes its pid file before exiting.
I know that unfortunately there are quite a few daemons etc. that don't tidy up in this respect.
What you could do in such a case was to send the process a SIGNULL (provided you are permitted to signal it, either because you are root or the same user) to check if it is still alive, or if it is a stale pid file.
e.g.

# kill -0 $(cat /var/run/suspicious_proc.pid) || rm -f /var/run/suspicious_proc.pid
Madness, thy name is system administration
Matti_Kurkela
Honored Contributor

Re: any idea how to check if .pid file abnormally terminated or not?

To test the existence of a file:

if [ -e /somewhere/maybe.pid ]; then
echo "maybe.pid exists"
else
echo "maybe.pid does not exist"
fi

About abnormal terminations and exit codes:

In Unix, the exit code is given to the parent process of the exiting process. The parent process may be programmed to store the exit code somewhere, or to ignore it.

If a parent process dies while its child processes are still running, the child processes are adopted by process number 1 (init, the (grand)mother of all processes).
Unfortunately, init will not log the exit codes of adopted processes anywhere.

If you want to catch the exit code of a process, you must set up a script or something else to watch for it.

An example watcher script:
-----------
#!/bin/sh
LOGFILE=/somewhere/appmonitor.log

# start the process you wish to monitor
/somewhere/someprogram maybe-some-options

# at this point, the process has terminated.
# Catch the exit code.
EXITCODE=$?
if [ $EXITCODE -ne 0 ]; then
echo "Abnormal termination: code $EXITCODE" >>$LOGFILE
else
echo "Normal termination" >>$LOGFILE
fi
-------------
MK
MK