Jun 23, 2015

Linux: Keep The Things Running With "Screen"

As a Linux user or developer, sometimes you need run a process locally in the current terminal or even remotely using a SSH connection, and keep it running after you close the terminal or the SSH session. However, if you put the process in background, it will be automatically stopped when you close the SSH connection or terminal. So how can we get out of terminal but keep the process running? "Screen" is the solution!

Let's use the "nano" terminal text editor as example, how could we keep it open/running after close the terminal? run with "nano &" ? simply "nano" and then ctrl+z ? "nano" and then close the terminal ? no! none of these methods will work, and that's why we need some way to manage this situation, like the "Screen". To start install it:

apt-get install screen

The basic syntax to create a new terminal is "screen -R <terminal name>":

screen -R test

Hit <enter> and then you should see a new terminal, like any other, so let's run the nano text editor:


Leave the terminal pressing CTRL+A and D, that's it! Now the screen has a terminal named "test" running the nano text editor, to list all the screen terminals type "screen -ls":

screen -ls
There is a screen on:
 4320.test (18-06-2015 18:12:15) (Detached)

Now you are free to close the current terminal or SSH connection, the nano editor will keep running. To restore the terminal screen, the syntax is "screen -r <terminal name>", so in this example to restore the terminal with the nano editor running we must type "screen -r test".

For a gracefully exit of the terminal, you can restore it, leave the nano editor and type "exit" to close the terminal, then the "screen -ls" will return nothing. For a fast and non-gracefully exit, you can kill its pid, in this example would be "kill -9 4320", but then the "screen -ls" would show this:

screen -ls
There is a screen on:
 4320.test (18-06-2015 17:37:28) (Dead ???)

Then you need clean from the list with "screen -wipe". The video below show this example, but running the nano editor remotely, closing the SSH session, restoring it and closing the screen terminal gracefully:

That's all!

About the versions
  • Ubuntu Linux 14.04.2 LTS
  • Screen version 4.01.00devel (GNU) 2-May-06

0 comentários :

Post a Comment