Results 1 to 3 of 3
So I have this 3rd party program that can run commands on the server (command line in unix).
The commands I want this 3rd party program to run on the ...
- 04-09-2008 #1Just Joined!
- Join Date
- Apr 2008
- Posts
- 1
Help Running Programs In Sequential Order
So I have this 3rd party program that can run commands on the server (command line in unix).
The commands I want this 3rd party program to run on the server are to run 3 different programs
in order (order matters).
The first program takes a input file and then outputs a temp file.
Then the second program takes that temp file that program 1 created and uses that as input and produces another temp file. Finally the third program thats program 2's temp file output as input and outputs a final useful file.
The problem is that it will not wait for program 1 to finish before running program 2 so the file that it takes as input might not exist yet. How do I check or make it so that program 2 will not start until program 1 has finished? If I put these commands in a shell script and run the shell script, will it run the programs in order and wait for each program to finish before executing the next?
- 04-10-2008 #2Linux Guru
- Join Date
- Oct 2007
- Location
- Tucson AZ
- Posts
- 1,884
In a bash script, separate the commands with a semi-colon.
command1;command2;command3
- 04-12-2008 #3Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
The stuff in a shell script is run sequentially. There's no need to use foo;bar, you can just use
That is, as long as the programs doesn't fork themselves to the background. Usually, that doesn't happen, unless you explicitly tell them to do so with the '&'.#!/bin/sh
foo
bar
For example, if foo is a normal program, if you run "foo" on command line, it will lock the shell until it ends the execution. So, anything after that command on the script, will not be run until this command ends.
But if you did "foo&", the program would be launched and automatically put into the background (it will continue running, but it will not lock the shell). So, the script will continue running the next command.
This is the normal behavior. But some programs fork themselves automatically, even if you don't put the trailing '&'.
In those cases (either if the program goes to background itself, or you use '&'), you might need to take another approach. For example, capture the PID of the process and then use the wait command:
#!/bin/bash
#We launch glxgears on the background
glxgears&
#Capture its PID
pid="${!}"
#This waits until the program with the given PID ends
wait $pid
#Here we put more instructions, these will not be run until
#the wait command above unlocks the shell
echo "glxgears was closed, it's pid was $pid"


Reply With Quote

