LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to write script that know when a program had ended (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-write-script-that-know-when-a-program-had-ended-671166/)

hchoonbeng 09-19-2008 10:57 PM

how to write script that know when a program had ended
 
Hi need to find out how to write script that will know when a command had ended its execution. e.g

for ( A = 1; A < 10 )

execute test;

end of test then increment value of A;

end for

thanks

matthewg42 09-20-2008 03:47 AM

Assuming you want to write a bash script, you can find the syntax of the for look in the bash manual page:

Quote:

for (( expr1 ; expr2 ; expr3 )) ; do list ; done

First, the arithmetic expression expr1 is evaluated according to the rules described below under ARITHMETIC EVALUATION. The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero. Each time expr2 evaluates to a non-zero value, list is executed and the arithmetic expression expr3 is evaluated. If any expression is omitted, it behaves as if it evaluates to 1. The return value is the exit status of the last command in list that is executed, or false if any of the expressions is invalid.

almatic 09-20-2008 04:23 AM

one way to test if program has ended is to look, if the process is still running. You could grep the process list or look for /proc/pid.

Code:

i=0
while [  "$i" -lt 10 ]
do
    sh yourprogram &   
    pid=$(pgrep yourprogram)
    if [ ! -d /proc/$pid ]
    then
          let (i+=1)
    fi
done

The above is a translation of your pseudo code and does not really make sense (as it starts yourprogram in the loop), but since I don't know what you intend to do, it might give you an idea at least.

i92guboj 09-20-2008 05:30 AM

The right way to do this in bash depends on whether your chosent test forks to the background or locks stdin.

Most programs will not return the control to the shell until they have finished, unless you explicitly fork them to the background by using the ampersand operator, like in:

Code:

$ startx&
If one program locks the shell, then you don't have to do anything special, since the shell script will not continue until the test program has finished.

Code:

for <whatever>
do
  # tests and stuff, the script should lock here until this ends
done

If the program doesn't lock or you just want to launch it in the background using '&' so you can do other stuff meanwhile, you can capture the pid, and use it to wait for it later.

Code:

for <whatever>
do
  my_test_command&
  pid=$! #this catches the pid of the last command
  # do anything else if you want
  wait $pid #this will lock here until the pid goes away, even if that means hanging forever
done

This has an advantage over pgrep and friends, because pgrep can find any process matching the string you give it. So, you can't guarantee that the correct one is picked if there are many of them running. I generally dislike this kind of approach, as much as I dislike the killall command for similar reasons.


All times are GMT -5. The time now is 01:18 PM.