• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

UNIX: question about nohup, nice, and shell scripts

pringlesCan

Junior Member
I would like to know how to do the following.

I have created a shell script that runs a series of simulations. These simulations take hours, so naturally I want to run them in the background and "nohup" them.

Moreover, I would like to do the following: run 5 simulations in this manner, and when those 5 simulations are complete, run the next 5 simulations.

I understand I can use the "wait" command to pause a script until a certain process completes. However, I have a problem trying to use the "wait" command in a script which is "nohup".

If I "'nohup" a script, do I have to "nohup" all commands it invokes? Otherwise the whole script is killed when I kill the session? Does anyone know how to accomplish something like this?

Very simple example

for i in 1 2 3 4 5
do
simCmd -args1 &
pid1=$!
simCmd -args2 &
pid2=$!
simCmd -args3 &
pid3=$!
simCmd -args4 &
pid4=$!
simCmd -args5 &
pid5=$!

wait $pid1
wait $pid2
wait $pid3
wait $pid4
wait $pid5

echo "5 Simulations have completed"
mv simResults* simResultsDir/.

done

Any help is appreciated. Thanks!
 
If you nohup the script, it should be fine. I think it equates to nohupping the shell interpretting the script.

I don't know much about wait...
 
If you use 'screen' you can avoid using nohup. And according to the builtins(7) page for 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.

So you should be able to just run the sims with the & and then run wait, once wait returns you know all the previous jobs have completed.
 
If I "'nohup" a script, do I have to "nohup" all commands it invokes?

No you don't have to.
As you know, 'nohup' causes the target process ignore hangup (SIGHUP) signal,
and signal setting will be inheritated by all child processes.

Commands in a shell script are execuated as child processes, unless it's a "build-in command".
In your case, "simCmd" will be a child process, "wait" is a build-in command and therefore running in the shell process itself. All of them will ignore SIGHUP.


Otherwise the whole script is killed when I kill the session?

You mean when you kill the login shell.
Strictly speaking, the term "session" includes all the process groups (i.e. all processes ) in your case, because you're not creating any daemons, which would have a new session id.
But I know what you mean 🙂

As suggested above, 'wait' will wait for all active jobs.
In general, always specify which shell you're using for the script.

Either "screen" or "VNC" could be useful in your case, you won't need to nohup then.
 
I have not tried in a script, but the '&&' command is conditional to the previous operation's completion. I wonder if that could be used somehow?
 
Thanks for the replies everyone. I ended up using a script that looked something like this. I ran the script with "nohup nice -10 simScript > log.txt". It all seems to work 🙂
 
Back
Top