Using screen to leave processes running in a terminal

If you are using an SSH connection and run a command that takes very long to process you can use nohup to prevent that process to be terminated when you logout from the SSH session:

$ nohup command > /dev/null 2>&1 &

But using this method detaches the process from stdio, stdout and stderr (unless you redirect them somewhere, as previous example redirected them to /dev/null). If you want to be able to reattach to that process later screen is the right tool.

First install screen:

$ sudo apt-get install screen

Then run a bash shell using screen:

$ screen bash

If you get an error like this:

Cannot open your terminal '/dev/pts/1' - please check

As explained here it may be because another user started the original shell (with sudo su or sudo -u username bash -l for example). Just run this command to own the shell:

$ script /dev/null

If everything goes well new shell will be opened and there you can run anything you want. Lets suppose you start converting a huge video to another codec. Once the process has started you have to press CTRL+a and then d to detach from that shell. Now you’ll be back to the original shell but the video process will be still running.

$ screen bash
[detached from 769.ttys001.macbook]

If you have only one screen process running you can reattach to it by just running this:

$ screen -r

If you have multiple screen processes at the same time you have to specify which of them you want to attach to. Using this command will tell you the names and some more info about all current screens:

$ screen -list
There is a screen on:
	769.ttys001.macbook	(Detached)
1 Socket in /tmp/screens/S-username.

Then you can attach to them using that name:

$ screen -r 769.ttys001.macbook

If you want to terminate a screen process just attach to it and press CTRL+D.

screen

You can configure some options of screen in ~/.screenrc. In this example the welcome message is disabled and a text is always shown at the bottom with the time, date and the total number of screens.

startup_message off
hardstatus alwayslastline '%{= wk}%?%-Lw%?%{r}(%{k}%n*%f%t%?(%u)%?%{r})%{k}%?%+Lw%?%?%= %d-%m-%Y %c'
activity ''

Source: http://raspi.tv/2012/using-screen-with-raspberry-pi-to-avoid-leaving-ssh-sessions-open
http://www.systerminal.com/2014/07/12/raspberry-pi-como-servidor-de-descargas-rtorrent-screen/

You might also like

Avoid SSH connection timeout that makes terminal window freeze
While using SSH to my server from Mac OS X (via iTerm2) I didn't have any problems, but when I connected...

Fix black screen in Raspberry Pi with HDMI to VGA adapter
If you use an HDMI to VGA adaptor and get a black screen you may tweak the config.txt to solve the problem.If...

Run a command from a Java application dealing properly with stdin, stdout and stderr
If you want to run an external command from a Java application you have to deal properly with the input,...

Using MacPorts php from command line
Some time ago I had installed Apache+MySQL using MacPorts in Mac OS X. It was all OK as long as I used...

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.