View Full Version : Screen -d -m

8th July 2005, 06:33 AM
Screen is best described as a terminal multiplexer. Using it, you can run any number of console-based applications--interactive command shells, curses-based applications, text editors, etc.--within a single terminal. A tutorial on screen can be seen here. (http://www.kuro5hin.org/story/2004/3/9/16838/14935)

Recently, I've changed Klaatu's (running Irssi (http://irssi.org/)) and HAL's (an infobot (http://infobot.org)) cron'd shell scripts to start up in a detached screen session by using screen -d -m

From screen's man page:

-d -m Start screen in "detached" mode. This creates a new session but
doesn't attach to it. This is useful for system startup

The shell script I use to keep Klaatu in #apple is:


PROCESSES=`ps -aucx | grep -c irssi`

if [ $PROCESSES -eq 0 ]; then
/usr/bin/screen -d -m /usr/local/bin/irssi


************************************************** ***************************

The above shell script checks to see if irssi's process is running and if not launches irssi in a detached screen window. It's run every 5 minutes in crontab:

*/5 * * * * davidperez /usr/local/bin/irssi.sh

************************************************** ***************************

I wrote a similar shell script for HAL with a couple of additions:


PROCESSES=`ps -aucx | grep -c perl`

if [ $PROCESSES -eq 0 ]; then
sleep 15;cd ~/infobot; /usr/bin/screen -d -m ./infobot

elif [ $PROCESSES -ge 3 ]; then
killall perl

************************************************** ***************************

HAL's shell script is also cron'd every 5 minutes. The sleep 15 is so that the shell script will execute 15 seconds after Klaatu's ensuring that Klaatu and HAL won't try to join #apple at the same time causing a connection flood.

The elif argument kills HAL's process if he joins #apple twice (e.g. after a netsplit). Sometimes, when HAL is "thinking", he runs two perl processes. Hence, greater than or equal to 3 rather than 2.

Irssi is a bit of a bother to configure, so for those interested, you can see Klaatu's configuration file here. (http://zarabeth.com/irssi_config.txt)

************************************************** ***************************

I also run bmon (http://people.suug.ch/~tgr/bmon/) from a similar shell script. Bmon needs to run in an interactive terminal window and won't run if simply executed in crontab.

************************************************** ***************************

To get the pid's of the detached terminal windows, type: screen -d -r:

[Colossus:~] davidper% screen -dr
There are several suitable screens on:
20154..Colossus (Detached)
2535..Colossus (Detached)
684..Colossus (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

To attach one of the windows, type: screen -d -r 20154 (for example)

9th August 2005, 11:38 AM
screen rocks..

One of the best terminal applications. Very very handy :)