sleepsort
sleepsort

Reputation: 1331

How to switch a sequence of tasks to background?

I'm running two tests on a remote server, here is the command I used several hours ago:

% ./test1.sh; ./test2.sh

The two tests are supposed to run one by one.If the second runs before the first completes, everything will be in ruin, and I'll have to restart the whole procedure.

The dilemma is, these two tasks cost too many hours to complete, and when I prepare to logout the server and wait for the result. I don't know how to switch both of them to background... If I use Ctrl+Z, only the first task will be suspended, while the second starts doing nothing useful while wiping out current data.

Is it possible to switch both of them to background, preserving their orders? Actually I should make these two tasks in the same process group like (./test1.sh; ./test2.sh) &, but sadly, the first test have run several hours, and it's quite a pity to restart the tests.

An option is to kill the second test before it starts, but is there any mechanism to cope with this?

Upvotes: 0

Views: 92

Answers (2)

perreal
perreal

Reputation: 97948

First rename the ./test2.sh to ./test3.sh. Then do [CTRL+Z], followed by bg and disown -h. Then save this script (test4.sh):

while :; do
  sleep 5;
  pgrep -f test1.sh &> /dev/null
  if [ $? -ne 0 ]; then
   nohup ./test3.sh &
   break
  fi
done

then do: nohup ./test4.sh &.

and you can logout.

Upvotes: 1

miku
miku

Reputation: 188014

First, screen or tmux are your friends here, if you don't already work with them (they make remote machine work an order of magnitude easier).

To use conditional consecutive execution you can write:

./test1.sh && ./test2.sh

which will only execute test2.sh if test1.sh returns with 0 (conventionally meaning: no error). Example:

$ true && echo "first command was successful"
first command was successful

$ ! true && echo "ain't gonna happen"

More on control operators: http://www.humbug.in/docs/the-linux-training-book/ch08s01.html

Upvotes: 0

Related Questions