Unix questions - Waiting on end of program execution from shell script?

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > February 2005 > Waiting on end of program execution from shell script?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Waiting on end of program execution from shell script?
Matthias Zach

2005-02-15, 8:53 pm

From a shell script I want to execute a Java program (e.g. myprog.class)
among other commands.

How do specify the program call statement in this script that:

1.) the further shell script execution should wait until the Java program finishes

2.) the further shell script execution should not wait for the Java program end
but resume other commands immediately after Java program call

3.) the further shell script execution should wait at most 3 minutes for the
end of the Java program run. If it is still running it should jump into
another branch of shell scripts (e.g. echo "Program not terminated after 3 minutes")

4.) the further shell script execution should continue with different shell commands
depending on the exit code of the Java program (e.g. exit(3)->echo "Exit code = 3")

Peter

Alan Connor

2005-02-15, 8:53 pm

On Wed, 16 Feb 2005 01:41:06 +0100, Matthias Zach
<zach228@fortuencity.com> wrote:


> From a shell script I want to execute a Java program (e.g.
> myprog.class) among other commands.
>
> How do specify the program call statement in this script that:
>
> 1.) the further shell script execution should wait until the
> Java program finishes


Assuming bash. (you should have specified what shell you are
using)

myprog.class &&

>
> 2.) the further shell script execution should not wait for the
> Java program end but resume other commands immediately after
> Java program call


myprog.class &

>
> 3.) the further shell script execution should wait at most 3
> minutes for the end of the Java program run. If it is still
> running it should jump into another branch of shell scripts
> (e.g. echo "Program not terminated after 3 minutes")
>



myprog.class &

sleep 3

if ps | grep 'myprog\.class'
then <commands>
else <commands>
fi


> 4.) the further shell script execution should continue with
> different shell commands depending on the exit code of the java
> program (e.g. exit(3)->echo "Exit code = 3")
>
> Peter
>


if [ "$?" = "3" ]
then <commands>
elif [ "$?" = "2" ]
then <commands>
elif ...
then ...
else
fi


HTH

AC




Barry Margolin

2005-02-16, 7:55 am

In article <GExQd.1338$IU.64@newsread3.news.pas.earthlink.net>,
Alan Connor <zzzzzz@xxx.yyy> wrote:

> On Wed, 16 Feb 2005 01:41:06 +0100, Matthias Zach
> <zach228@fortuencity.com> wrote:
>
>
>
> Assuming bash. (you should have specified what shell you are
> using)
>
> myprog.class &&


Why do you need '&&'? The normal behavior of virtually all shells is to
wait for a command to finish before executing the next command. No
special syntax is needed.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Alan Connor

2005-02-16, 7:55 am

On Wed, 16 Feb 2005 00:09:46 -0500, Barry Margolin
<barmar@alum.mit.edu> wrote:

> In article <GExQd.1338$IU.64@newsread3.news.pas.earthlink.net>,
> Alan Connor <zzzzzz@xxx.yyy> wrote:
>
>
> Why do you need '&&'? The normal behavior of virtually all
> shells is to wait for a command to finish before executing the
> next command. No special syntax is needed.
>


That's what I was taught to use by the other pros on
comp.unix.*, when one wants to make sure that the previous
command has completely finished before the next one was executed.

But you have inspired me to take a look at man bash, and it turns
out that '&&' is the logical AND operator and only executes the
second command if the first one exits with an exit code of zero.

Which is a bit of a different thing and could definitely cause
problems with the script that the OP is apparently trying to
write, where he may be checking for other exit codes from the
first program.

For the OP: The '||' operator will execute the second
command only if the first one exits with a non-zero exit code.

Good call, Barry.

Thanks.

AC
Alan Connor

2005-02-16, 7:55 am

On Wed, 16 Feb 2005 01:49:26 GMT, Alan Connor <zzzzzz@xxx.yyy> wrote:
>
>
> On Wed, 16 Feb 2005 01:41:06 +0100, Matthias Zach
><zach228@fortuencity.com> wrote:
>
>
>
> Assuming bash. (you should have specified what shell you are
> using)
>
> myprog.class &&
>
>
> myprog.class &
>
>
>
> myprog.class &
>
> sleep 3


Oops. That would be 'sleep 180'.

<snip>

AC

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com