Stateless process | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Stateless process

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2013
MDominok's Avatar
MDominok MDominok is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 27 September 2013, 10:37 AM EDT
Location: Duisburg, Germany
Posts: 12
Thanks: 8
Thanked 1 Time in 1 Post
Stateless process

Hi Folks

I'm trying to monitor that a process is running, using ps.

Astonishingly the process, which is checked every 15 minutes, is runnnig but without a state about 2-3 times a day.

Extract from the script :

Code:
#!/bin/ksh

# edii_pid is PID of process to monitor.

# Checking if pid is listed in hosts processes.
# tr used for getting rid of potential leading blanks.
edii_pid_listed=`ps -p ${edii_pid} -o pid | grep -v "^  PID" | tr -d ' '`

if [[ ${edii_pid_listed} != ${edii_pid} ]]; then
  # So PID is listed, exists. In fact the process/application is runnig
  # for several days continuously.

  # Checking if pid is in running (=0) state.
  # tr used for getting rid of potential leading blanks.
  edii_pid_runnning=`ps -p ${edii_pid} -o s | grep -v "^S" | tr -d ' '`
  if [[ ${edii_pid_runnning} != 'O' ]]; then
    print "${edii_pid} isn't in running (O) state but ${edii_pid_runnning}!" 
  fi
fi

And for about 2-3 times a day i get this :

Code:
PID 2939 isn't in running (O) state but !

Any hints why a process that is running for days, has been "seen" in list of processes only microseconds before querying the state is stateless?

System is SunOS 5.10 Generic_148888-03 sun4v sparc SUNW,SPARC-Enterprise-T5220

Cheers

Michael
Sponsored Links
    #2  
Old 09-13-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 September 2014, 9:41 AM EDT
Location: NM
Posts: 10,219
Thanks: 278
Thanked 800 Times in 744 Posts
I am confused by what you say you see. Processes either exist in the kernel process masthead or they do not exist. A non-running (terminated) process that has not been waited for is a zombie - the other possible states are states like sleep, swapped out, some kind of wait state, running, etc. There is no "stateless" value presented by ps.

I also do not get what your code is supposed to be doing.

If you want to check process existence try something like this:

Code:
# the 0 is a zero and does not harm the process, you have to be root or same user
# to signal any process
   kill 0 $pid && echo "$pid is alive" || echo "$pid is not alive"


Last edited by jim mcnamara; 09-13-2013 at 09:40 PM..
The Following User Says Thank You to jim mcnamara For This Useful Post:
MDominok (09-16-2013)
Sponsored Links
    #3  
Old 09-14-2013
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 30 September 2014, 10:26 AM EDT
Location: /dev/ph
Posts: 4,811
Thanks: 63
Thanked 425 Times in 394 Posts
Suggest you modify your script to better capture the raw process state. Something like the following (which I have not tested):

Code:
edii_pid_runnning=`ps -p ${edii_pid} -o s`
edii_pid_runnning_state=`echo ${edii_pid_runnning} | grep -v "^S" | tr -d ' '`
if [[ ${edii_pid_runnning_state} != 'O' ]]; then
    print "${edii_pid} isn't in running (O) state but is ${edii_pid_runnning}!" 
fi

The Following User Says Thank You to fpmurphy For This Useful Post:
MDominok (09-16-2013)
    #4  
Old 09-14-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 29 September 2014, 1:02 PM EDT
Location: Simplicity
Posts: 1,924
Thanks: 123
Thanked 561 Times in 509 Posts
I guess the state becomes S (sleeping) that you are discarding with grep.
Improvement:

Code:
edii_pid_runnning=`ps -p ${edii_pid} -o s=`

And

Code:
edii_pid_listed=`ps -p ${edii_pid} -o pid=`

The = omits the ps header.
Leading spaces are removed by the shell if you use the variable unquoted in commands like

Code:
if [[ ${edii_pid_listed} != ${edii_pid} ]]; then


Last edited by MadeInGermany; 09-14-2013 at 10:45 PM..
The Following User Says Thank You to MadeInGermany For This Useful Post:
MDominok (09-16-2013)
Sponsored Links
    #5  
Old 09-16-2013
MDominok's Avatar
MDominok MDominok is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 27 September 2013, 10:37 AM EDT
Location: Duisburg, Germany
Posts: 12
Thanks: 8
Thanked 1 Time in 1 Post
Quote:
Originally Posted by MadeInGermany View Post
I guess the state becomes S (sleeping) that you are discarding with grep.
Improvement:

Code:
edii_pid_runnning=`ps -p ${edii_pid} -o s=`

And

Code:
edii_pid_listed=`ps -p ${edii_pid} -o pid=`

The = omits the ps header.
Leading spaces are removed by the shell if you use the variable unquoted in commands like

Code:
if [[ ${edii_pid_listed} != ${edii_pid} ]]; then

Yep, that was it.

Added some more debug lines to the script and noticed that the process (S)leeps from time to time. Which is perfectly normal for unix.

So, puh, the world still _is_ a sphere after all. Just the usual PEBKAC. 8-)

Thanks all for replying!
Sponsored Links
    #6  
Old 09-16-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 29 September 2014, 1:02 PM EDT
Location: Simplicity
Posts: 1,924
Thanks: 123
Thanked 561 Times in 509 Posts
A further improvement:
In Solaris and Linux you can simply test for a known PID like this

Code:
if [[ -d /proc/${edii_pid} ]]; then
 echo "still alive"
else
 echo "dead"
fi

The Following User Says Thank You to MadeInGermany For This Useful Post:
MDominok (09-18-2013)
Sponsored Links
    #7  
Old 09-16-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 30 September 2014, 3:49 PM EDT
Location: Saskatchewan
Posts: 19,477
Thanks: 794
Thanked 3,283 Times in 3,078 Posts
and on systems that don't, I believe you can just do


Code:
ps $pid >/dev/null 2>/dev/null || echo "$pid is dead"

The Following User Says Thank You to Corona688 For This Useful Post:
MDominok (09-18-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Protect service with statefull, stateless nini Homework & Coursework Questions 1 04-27-2012 07:54 AM
script to monitor the process system when a process from user takes longer than 15 min run. kirankrishna3 Shell Programming and Scripting 1 09-22-2010 06:11 AM
Shell Script to Kill Process(number of process) Unix/Solaris jonnyvic Shell Programming and Scripting 2 12-03-2009 01:44 PM
script to monitor process running on server and posting a mail if any process is dead pradeepmacha Shell Programming and Scripting 13 03-06-2009 06:33 AM



All times are GMT -4. The time now is 03:53 PM.