LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-19-2008, 10:57 PM   #1
hchoonbeng
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

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

Rep: Reputation: 65
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.
 
Old 09-20-2008, 04:23 AM   #3
almatic
Member
 
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.

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.
 
Old 09-20-2008, 05:30 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

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

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


Reply



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 06:05 PM
How to write script with check running program VooZ2_uk Programming 3 12-13-2006 09:57 AM
How to Write Script Program as Deamon bogoda Linux - Software 7 01-21-2006 12:14 AM
HELP:write a CGI program with shell script supermyself Programming 3 06-12-2005 03:10 PM
Starting program remotely and have them continue to run after the session is ended OJAtkinson Linux - General 2 12-07-2004 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:22 AM.

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