2

So I have a bash script that first asks for a series of three variables from the user. I then want the script to run in the background disconnected from my session, so that when I cut the connection between my laptop and the supercomputer, the script will still run.

The variables are asked for using echo with code that looks like this:

echo "What is limit for low level optimization?" #ask for low limit
read low
echo "What is limit for high level optimization?" #ask for high limit
read high
echo "What method?" #ask for method
read input 

It then goes on to execute a long series of commands.

However I cannot figure out how to successfully disconnect this job from my session. The "disown" command didn't work last time as the files that should have been generated on the steps after I logged out weren't created.

From what I understand, nohup won't work here because that would prevent it from reading any of the input the script asked me for in the beginning.

So is there a way to set this up?

1

You can accomplish that by emulating what nohup does in the second part of your script.

The nohup command doesn't do more than:

  • Redirect the standard file descriptors, so that the script will not try to read or write to the terminal.

  • Trap the SIGHUP signal and ignore it. SIGHUP is what a process gets when its controlling terminal is closed. If it's ignored, the process will keep running after its terminal is closed.

You can accomplish that using the following script:

echo "What is limit for low level optimization?" #ask for low limit
read low
echo "What is limit for high level optimization?" #ask for high limit
read high
echo "What method?" #ask for method
read input 
(
    trap '' 1  # Ignore SIGHUP
    # Series of long running commands
    # using $low, $high and $input
) </dev/null >myscript.log 2>&1 &

The last line will redirect stdin from /dev/null and both stdout and stderr to a myscript.log (you can pick a better file name and possibly use a timestamp or similar to make it unique.)

Finally, you put the whole process in background with a final &.

One other possibility is to split your script in two, have the second one take low, high and input as command-line arguments, or possibly as exported environment variables, and then run the second part using an actual nohup command. That should be simpler and somewhat more flexible, in that you can script execution of the second part without having to deal with interactive prompts.

Another possibility is to use terminal multiplexers such as tmux or screen, so that you can reattach to your session if you get disconnected (or you can detach it yourself), also you can open a new window to run a new shell while your script keeps running on the initial window. Regardless of whether you implement one of the solutions above, adopting tmux or screen would be a good idea, so give it a try if you can.

0

You could wrap the long series of commands in curly braces and then use & to continue without waiting.

echo "Read input"
{
    # all your commands in curly braces
    # ...
} & exit 0

Then you can exit without waiting for all your commands to finish. It should continue running even if you disconnect from the server.

  • 1
    So, even after adding this I am still stuck in the jobs that are submitted. I have some jobs that get submitted via pbs as part of this script and then I can't get out of the submission lines for those jobs. – Pyrodancer123 Aug 12 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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