Sponsored Content
Top Forums Shell Programming and Scripting Monitor a long running process Post 302419507 by alister on Friday 7th of May 2010 11:15:40 AM
Old 05-07-2010
Quote:
Originally Posted by sunpraveen
I usually use while loop to monitor any long running process as below:

Code:
while true
do
if [[ $(ps -ef | grep <long running process id> | wc -l) -eq 0 ]];
then
<do something>
break
else
<do something>
fi

In my opinion, this is a very poor approach, as it is very vulnerable to false positives. What if a different command with a different pid contains an argument that matches the pid being grepped for? This code will not detect that the monitored process has died/exited. What's much worse, however, is that depending on the kernel process scheduler and the shell implementation, the grep command's pid argument itself could trigger a false positive when groveling through the ps output (and given the way this code is structured, to test for equality to zero as failure, this could be happening often ... silently).

I would (perhaps melodramatically Smilie) characterize this as an indiscriminate grepping of ps output. When dealing with multicolumn output whose values can collide (the arguments field, for example, can conceivably contain a username/pid/ppid/...), it is safer and easier to constrain the match to a specific column using AWK. And, if applicable, use ps' output options to limit output only to the fields that are necessary. If you are grepping for a pid, why not use a ps output format that only prints pid?

Instead of ...
Code:
if [[ $(ps -ef | grep <long running process id> | wc -l) -eq 0 ]];

I would suggest ...
Code:
if ! [ $(ps -o pid= -p <long running process id>) ]

Or, you could grep|wc the output of "ps -o pid= -p <long running process id>", if that feels more familiar Smilie

Regards,
Alister

Last edited by alister; 05-07-2010 at 12:36 PM..
 

9 More Discussions You Might Find Interesting

1. Programming

How to monitor if a process is running

I would like to know if i can monitor if a process is running. I have one program wich is running all the time, called oliba, but sometimes it goes down, and I have to launch it again. Is there a way to monitor the pid of the program, and if the program goes down, to lauch it again? Can you give... (3 Replies)
Discussion started by: Pedro Tavares
3 Replies

2. UNIX for Advanced & Expert Users

how to check how long the process has been running.

I have a requirement to check how long a process is running on unix system.If i use ps -ef i am getting the following message guest 2453638 1998920 0 16:16:05 - 0:00 dsapi_slave 9 8 0 but this is showing only time not the date.Can any one please advice me any script to find out how... (2 Replies)
Discussion started by: ukatru
2 Replies

3. UNIX for Advanced & Expert Users

How to check since when (for how long) a particular process is running ?

How do I check if a particular process is running from a certain date and time ? (2 Replies)
Discussion started by: hpuxlxboy
2 Replies

4. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

5. Shell Programming and Scripting

script to monitor the process system when a process from user takes longer than 15 min run.

get email notification from from system when a process from XXXX user takes longer than 15 min run.Let me know the time estimation for the same. hi ,any one please tell me , how to write a script to get email notification from system when a process from as mentioned above a xxxx user takes... (1 Reply)
Discussion started by: kirankrishna3
1 Replies

6. UNIX for Dummies Questions & Answers

Long running sessions

Hi, How can i find out the average cpu utilization of a particular long-running process in UNIX? is there some command for this Thanks (3 Replies)
Discussion started by: arunmanas
3 Replies

7. Shell Programming and Scripting

Check for long running process on AIX

I want to write a shellscript which determines if a particular process is long running than my specified threshold time. Eg: My process name is "prsd" and is expected to run for 15 mins and completes. If I set a threshold limit of 1 hour, and how we can the get output of the long running... (4 Replies)
Discussion started by: chandu123
4 Replies

8. Shell Programming and Scripting

Killing the process if running for long time in script

I am running a script which will read the data from fail line by line and call the Java program by providing the arguments from the each line. The Java code is working fast for few records and for some records its getting hanged not providing response for morethan one hour. Currently am... (4 Replies)
Discussion started by: dineshaila
4 Replies

9. Shell Programming and Scripting

Command to get exact tomcat process I am running ignoring other java process

Team, I have multiple batchjobs running in VM, if I do ps -ef |grep java or tomcat I am getting multiple process list. How do I get my exact tomcat process running and that is unique? via shell script? (4 Replies)
Discussion started by: Ghanshyam Ratho
4 Replies
platform::shell(n)					       Tcl Bundled Packages						platform::shell(n)

__________________________________________________________________________________________________________________________________________________

NAME
platform::shell - System identification support code and utilities SYNOPSIS
package require platform::shell ?1.1.4? platform::shell::generic shell platform::shell::identify shell platform::shell::platform shell _________________________________________________________________ DESCRIPTION
The platform::shell package provides several utility commands useful for the identification of the architecture of a specific Tcl shell. This package allows the identification of the architecture of a specific Tcl shell different from the shell running the package. The only requirement is that the other shell (identified by its path), is actually executable on the current machine. While for most platform this means that the architecture of the interrogated shell is identical to the architecture of the running shell this is not generally true. A counter example are all platforms which have 32 and 64 bit variants and where a 64bit system is able to run 32bit code. For these running and interrogated shell may have different 32/64 bit settings and thus different identifiers. For applications like a code repository it is important to identify the architecture of the shell which will actually run the installed packages, versus the architecture of the shell running the repository software. COMMANDS
platform::shell::identify shell This command does the same identification as platform::identify, for the specified Tcl shell, in contrast to the running shell. platform::shell::generic shell This command does the same identification as platform::generic, for the specified Tcl shell, in contrast to the running shell. platform::shell::platform shell This command returns the contents of tcl_platform(platform) for the specified Tcl shell. KEYWORDS
operating system, cpu architecture, platform, architecture platform::shell 1.1.4 platform::shell(n)
All times are GMT -4. The time now is 08:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy