mrclng
mrclng

Reputation: 543

Bash script to run a detached loop that sequentially starts backgound processes

I am trying to run a series of tests on a remote Linux server to which I am connecting via ssh.

Here's what I tried:

#!/usr/bin/env bash
# Assembling a list of commands to be executed sequentially
TESTRUNS="";
for i in `ls ../testSet/*`;
do 
  MSG="running test problem ${i##*/}";
  RUN="mySequentialCommand $i > results/${i##*/} 2> /dev/null;";
  TESTRUNS=$TESTRUNS"echo $MSG; $RUN"; 
done
#run commands with nohup to be able to log out of ssh session
nohup eval $TESTRUNS &

But it looks like nohup doesn't fare too well with eval. Any thoughts?

Upvotes: 0

Views: 1555

Answers (2)

Walter A
Walter A

Reputation: 20002

You could take a look at screen, an alternative for nohup with additional features. I will replace your testscript with while [ 1 ]; do printf "."; sleep 5; done for testing the screen solution.
The commands screen -ls are optional, just showing what is going on.

prompt> screen -ls
No Sockets found in /var/run/uscreens/S-notroot.
prompt> screen
prompt> screen -ls
prompt> while [ 1 ]; do printf "."; sleep 5; done
# You don't get a prompt. Use "CTRL-a d" to detach from your current screen
prompt> screen -ls
# do some work
# connect to screen with batch running
prompt> screen -r
# Press ^C to terminate the batch (script printing dots)
prompt> screen -ls
prompt> exit
prompt> screen -ls

Google for screenrc to see how you can customize the interface.

You can change your script into something like

#!/usr/bin/env bash
# Assembling a list of commands to be executed sequentially
for i in ../testSet/*; do
do 
  echo "Running test problem ${i##*/}"
  mySequentialCommand $i > results/${i##*/} 2> /dev/null 
done

Above script can be started with nohup scriptname & when you do not use screen or simple scriptname inside the screen.

Upvotes: 0

Chen A.
Chen A.

Reputation: 11280

nohup is needed if you want your scripts to run even after the shell is closed. so yes.

and the & is not necessary in RUN since you execute the command with &.

Now your script builds the command in the for loop, but doesn't execute it. It means you'll have only the last file running. If you want to run all of the files, you need to execute the nohup command as part of your loop. BUT - you can't run the commands with & because this will run commands in the background and return to the script, which will execute the next item in the loop. Eventually this would run all files in parallel.

Move the nohup eval $TESTRUNS inside the for loop, but again, you can't run it with &. What you need to do is run the script itself with nohup, and the script will loop through all files one at a time, in the background, even after the shell is closed.

Upvotes: 1

Related Questions