Process Detection and Conditional Branching . . .


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Process Detection and Conditional Branching . . .
# 1  
Old 02-10-2014
Process Detection and Conditional Branching . . .

Greetings!

First post for this newbie Smilie

If anyone has a moment to spare, I have a quick question for the community. For the record, I'm running Xubuntu 12.04...

Based on a few hours' digging about, I've created an executable test script, "test.sh" which incorporates the following code:

Code:
#!/bin/sh

runSeq() 
{
    sleep 1
    if ps -ef | grep -v grep | grep "some-process-name" > /dev/null
    then
    zenity --warning
    holdSeq
    else
    runSeq
}
runSeq

holdSeq()
{
    sleep 1
    if ps -ef | grep -v grep | grep "some-process-name" < /dev/null
    then
    holdSeq
    else
    runSeq
}
holdSeq

The object is to check every second to determine if a certain process is running; and, if it is, fire off a warning dialog for the user to dismiss. The script is then to wait around in the background until the process has been closed by the user; at which time it returns to its original standby mode.

However, for the life of me, I can't seem to get the above code to work. Gotta be a drop-dead simple thing syntaxwise; but it simply has eluded me thus far...

Any suggestions for the noob?

Thanks a bunch!

Last edited by LinQ; 02-10-2014 at 05:14 PM.. Reason: Code spiff ;)
# 2  
Old 02-10-2014
I'm not sure I understand what you want to achieve with your script. Calling those functions recursively without an exit branch is not the right way. Some point in time that will blast the stack. Why don't you create one single loop with an exit condition, and in that loop you sleep, test for that process, and run zenity warning if condition met?
BTW, you second redirection seems wrong.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-10-2014
Have you tried running the script in debug mode? Might help you figure out what's causing the issue.

Code:
sh -x test.sh

This User Gave Thanks to in2nix4life For This Post:
# 4  
Old 02-10-2014
Thanks, folks.

With the collective input, I got this to work well:
Code:
#!/bin/sh

runSeq() 
{
    sleep 1
    if ps -ef | grep -v grep | grep "some-process-name" > /dev/null
    then
    zenity --warning
    holdSeq
    else
    runSeq
    fi
}
runSeq

holdSeq()
{
    sleep 1
    if ps -ef | grep -v grep | grep "some-process-name" > /dev/null
    then
    holdSeq
    else
    runSeq
    fi
}
holdSeq

However, now I'm really concerned about what RudiC said: Will I wind up running off the stack with the above code? Seems like it's all just going about in a slow, quiet loop.

What am I missing Smilie

Thanks again --
# 5  
Old 02-10-2014
Your calls to holdSeq and runSeq are not like goto statements they are function calls which push the previous state onto a stack the flow of control never reaches the end of either of these functions before calling another instance.

Consider this alternative:

Code:
runSeq()
{
 while :
 do 
   ps -ef | grep -q "[s]ome-process-name" && zenity --warning
   while ps -ef | grep -q "[s]ome-process-name"
   do
     sleep 1
   done
   while ! ps -ef | grep -q "some-process-name"
   do
      sleep 1
   done
  done
}

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 02-11-2014
@Chubler_XL:
Thanks for the heads up here. Good code...

"Thanks" for everyone, too Smilie

However, this raises one last question concerning the OP:
Out of curiosity, is there some way of resetting/clearing the stack in an instance like this with an explicit call?

Or, is there a garbage collection function which can be accessed and called on the fly???


Again, just a curious noob Smilie
# 7  
Old 02-11-2014
There is no magic function you can call to unwind a stack when your functions never return.

You could overlay the current process with a fresh copy of itself by replacing the lines at the end of your file:
Code:
    else
    runSeq
    fi
}
holdSeq

with:
Code:
    else
    exec $0
    fi
}

Note that the last line of this script will never be reached and can, therefore, be removed and not affect the behavior of this script.

Obviously, Chubler_XL's script is a much better approach. Unless each recursive call in your original script is replaced by an exec call, there is no guarantee that the appropriate state changes will occur to get you to an exec call before you run out of stack space.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add conditional check of whether a process is running before doing rest of script?

Hi, I'm looking at doing a cron job for a script that could take a very short time to complete or a very long time to complete based on variable activity on a server. I don't want to do a weird schedule, but I don't want to be so aggressive that the process attempts to start before another... (14 Replies)
Discussion started by: nbsparks
14 Replies

2. Shell Programming and Scripting

File delta detection

Hello, I need to compare two flat files (ASCII format), say file OLD and file NEW. Both have similar structure. These files are | delimitted files and have around few million of records (lines) each. Each file has same set columns and same set of key columns (i.e. the 3rd and 5th column of the... (7 Replies)
Discussion started by: manubatham20
7 Replies

3. Programming

Parallel Processing Detection and Program Return Value Detection

Hey, for the purpose of a research project I need to know if a specific type of parallel processing is being utilized by any user-run programs. Is there a way to detect whether a program either returns a value to another program at the end of execution, or just utilizes any form of parallel... (4 Replies)
Discussion started by: azar.zorn
4 Replies

4. Shell Programming and Scripting

NAT detection

hellou, can anybody help me with nat detection in real time ? i prefer some detection script because i try some nat detection program's for example p0f or i'm using tcpdump, but i would get contain of specific packet. Some ideas? (1 Reply)
Discussion started by: TheTechnic
1 Replies

5. Shell Programming and Scripting

begin end detection

Hi all, i am new to scripting. i need to write a code to detect begin and end of word that either begins or ends with t,th,d,dh,s,sh i have a set of words in a file containg one word per line. let the filename be aaa.txt. i have an another file bbb.txt which has two lines, just specifying the... (7 Replies)
Discussion started by: blkanth
7 Replies

6. IP Networking

modem detection

How to get information that where is my modem configured in /dev. I have two modems configured in my device .. one is USB and other is PCI modem.. USB is detected as /dev/USB0. but how to see about PCI modem? (0 Replies)
Discussion started by: s123.radha
0 Replies

7. Shell Programming and Scripting

Looping and conditional branching on the command line

I am piping STDOUT from commands such as ifconfig and dmesg through grep, sed and awk to get the information I need. I need to now perform some looping and branching now and have been trying to figure out how to do this on the command line. You may ask "Why the command line? - Why not put it... (2 Replies)
Discussion started by: karlgo
2 Replies

8. UNIX for Dummies Questions & Answers

virus detection

IS there virus software for unix? I worked in a Solaris environment and dont remember having anything. I also ask because the current enviroment i am working on is Microsoft and they argue that they do not use unix because of virus detection. Any input would be greatly appreciated. (1 Reply)
Discussion started by: pbonilla
1 Replies
Login or Register to Ask a Question