Miguel Cardoso
Miguel Cardoso

Reputation: 69

Waiting a process to terminate to start another one

I wanted to take out the maximum performance of my machine. So, I attached all available colors to a single process, being this one dependent on an iteration.

#! /bin/bash
[ -z "`echo $BIN`" ] && BIN="mpirun -np 12 /home/path"
for i in 1 2 3 4 5;do
# setting input 
$BIN &
mv output_file output_file_$i
done

When I open my nohup.out file I got the message that my output_file is not there. I understand it but I want the process to finish, move that file and continue with different input parameters.

Upvotes: 0

Views: 67

Answers (2)

Mahmoud Odeh
Mahmoud Odeh

Reputation: 950

this would block your terminal until your process finished and would be considered a foreground process with no user interaction. The point here being whether the process blocks the execution of other processes until it terminates.

you can make a foreground process into a background one by adding & at the end of your command line.

#!/bin/bash
[ -z "`echo $BIN`" ] && BIN="mpirun -np 12 /home/path"
for i in {1..5}; do $BIN --output-file output_file_$i; done

Upvotes: 2

Ben XO
Ben XO

Reputation: 1219

It seems you are not waiting long enough between starting mpirun and moving the log file to a new name. These two lines:

$BIN &
mv output_file output_file_$i

This runs your BIN and then immediately tries to rename output_file, probably so quickly that the file has not even been created yet!

Instead of $BIN & why not $BIN --output-filename output_file_$i & ? And then you don't need the mv command at all.

So the whole script would become

#!/bin/bash
[ -z "`echo $BIN`" ] && BIN="mpirun -np 12 /home/path"
for i in 1 2 3 4 5; do
  $BIN --output-file output_file_$i &
done

Upvotes: 2

Related Questions