Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a situation where I run two commands (they go into background (nohup)), come back after 2 hours, and if both have completed, run two more commands (again nohup), again come back after 2 hours and run two more commands and so on..

I was wondering if it was possible for me to queue these up, so that after the first two finish, the next two start automatically, so that I don't need to keep checking every 2 hours?

All those are independent programs; they are simulations that are memory and CPU hungry, so I don't want more than two of them running simultaneously at any time.

I run them using a syntax similar to:

nohup mySimulator inputs1.txt
nohup mySimulator inputs2.txt
nohup mySimulator inputs3.txt [after the first two have completed, that is..]
share|improve this question
    
these two commands from the first half are running concurrently or what. Just give an example how do you start them with some 'meta-programs' –  Serge Oct 2 '12 at 18:35
    
Does the programs from second half depend on the results from the first half? –  Serge Oct 2 '12 at 18:43
    
No, all of them are independent of each other (the order doesn't matter).. Just that not more than two of them can run at a time.. –  Pradyumna Oct 2 '12 at 18:46
    
ok, I got the idea –  Serge Oct 2 '12 at 18:46

3 Answers 3

up vote 6 down vote accepted

write a shell script like this:

nohup mySimulator inputs1.txt
nohup mySimulator inputs2.txt
while (ps -A | grep -v grep | grep mySimulator > /dev/null); do sleep 1; done
nohup mySimulator inputs3.txt

Then run it from the shell

It fires up two simulators, then waits until there is no mySimulator on the process list and then continues.

(This was run on a Mac, so depending on your operating system it may need a little tweaking ... ps -A is supposed to print a list of all running processes)

share|improve this answer
    
Thank you for the idea.. I used sleep 10 instead of sleep 1 –  Pradyumna Oct 3 '12 at 16:09

use the wait command, but don't use nohup.

command1 &
command2 &
wait        # this waits for command1 and command2
command3 &
command4 &

from man bash:

wait [n ...] Wait for each specified process and return its termination status. Each n may be a process ID or a job specification; if a job spec is given, all processes in that job's pipeline are waited for. If n is not given, all currently active child processes are waited for, and the return status is zero. If n specifies a non-existent process or job, the return status is 127. Otherwise, the return status is the exit status of the last process or job waited for.

share|improve this answer
    
Thanks, akira. :) Nice addition. –  UtahJarhead Oct 2 '12 at 20:42

You can use the bash && operator. This is similar to the pipe | operator, except that it doesn't pipe the output to the next command. Instead, it chains them together so that the execution continues as long as one of the commands doesn't encounter an error.

For example:

echo "Test" && echo "Test done." && echo "Test2" && echo "Test2 done."

This will halt on a script error, or for any script that returns false. For example:

echo "Test" && echo "Test done." && echo "Test2" && echo "Test2 done." && false && echo "This will never trigger."
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.