How to keep things running after you close your terminal (Linux/Unix)28 Feb 2017
When you start playing with cloud computing like Amazon Web Services, you will, sometimes, decide to launch a program that will take a while to run. If you simply close the connection before all processes are finished, the system will terminate bash (or whatever shell you were using) and, therefore, your program will be also terminated. Normally, when we are working on a terminal, we make use of the
bg) to send things to the background freeing the terminal. If you sent your process to background, you will be able to use
jobs to display information about processes that are sleeping (the
ctrl+z thing) or running on the background.
The problem here is that your processes running on the background will be terminated anyway. There are at least two solutions. The simplest one is installing
nohup on your computer (in case it’s not already available):
$ sudo apt-get install nohup
nohup, you really just need to add it in front of your normal command and send it to the background (e.g.
$ nohup ls &). By default, a file called
nohup.out will be created with all the output (stdout/stderr) generated by your program. You can find more “nohup stuff” here.
The second option is to manually do what
nohup does, but using
disown. This is explained here. You will need to send your process to background (
bg), verify its job number with
jobs -l and then:
$ disown -h job_number_you_just_found
Today, I was launching some simulations on a server and I decided to do it using
&& to make sure the next step would only be executed if the previous had finished without errors. The solution, using
$ nohup sh -c 'first_command && second_command' &
That’s it. Have a happy cloud computing experience!