Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-19-2008, 11:57 PM   #1
LQ Newbie
Registered: Sep 2008
Posts: 15

Rep: Reputation: 0
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

Old 09-20-2008, 04:47 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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

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.
Old 09-20-2008, 05:23 AM   #3
Registered: Mar 2007
Distribution: Debian
Posts: 547

Rep: Reputation: 67
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.

while [  "$i" -lt 10 ]
     sh yourprogram &     
     pid=$(pgrep yourprogram)
     if [ ! -d /proc/$pid ]
          let (i+=1)
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.
Old 09-20-2008, 06:30 AM   #4
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
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:

$ 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.

for <whatever>
  # tests and stuff, the script should lock here until this ends
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.

for <whatever>
  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
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.

Last edited by i92guboj; 09-20-2008 at 06:34 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
write a C++ program yewjingcho Programming 14 11-01-2007 07:05 PM
How to write script with check running program VooZ2_uk Programming 3 12-13-2006 10:57 AM
How to Write Script Program as Deamon bogoda Linux - Software 7 01-21-2006 01:14 AM
HELP:write a CGI program with shell script supermyself Programming 3 06-12-2005 04:10 PM
Starting program remotely and have them continue to run after the session is ended OJAtkinson Linux - General 2 12-07-2004 01:06 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration