Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What's clean way to achieve, following functionality:

function screen_create_or_reuse(){
if [ running_screen_session "$1" ]; then
    reattach_screen_session screen_name_"$1"
else
    create_screen_session "$1" screen_config_"$1" screen_name_"$1"
fi
}

Currently, I have to worry if session is already runnning (screen -ls or screen -wipe). Depending on it, if it's not running: create session with custom configuration and name manually (screen -c config -S name) and than reattach manually. I'd like one command screen_create_or_reuse name which handles all those details.

I thought about creating bash script which detects presense of session etc., but I see screen has already a lot of options, maybe I would do overkill.

What's clean way of achieving above, with screen ?

share|improve this question
add comment

2 Answers

The following option is from man screen:

   -d -R   Reattach a session and if necessary detach or even create it first.

Use it with -S to set the session name.

share|improve this answer
    
is it realiable to use it with -c as well ? (-c for configuration) –  Grzegorz Wierzowiecki Nov 29 '11 at 10:45
    
Do I really have to use it with -S for name ? As I run screen -dr NAME -c conf it works ? So what's clean and reliable way ? –  Grzegorz Wierzowiecki Nov 29 '11 at 15:11
add comment

For the most part, -S foo -d -RR will do what you want: it attaches to the session whose name you specify, and creates one if one doesn't exist already.

I like to go a bit further, and look for name-specific configuration files. This lets me define different environment variables, startup applications, and so on for different session names.

This is my file etc/screen/0.screenrc, which is sourced by other session-specific configuration files:

source ~/.screenrc
unsetenv DISPLAY
msgminwait 0
msgwait 0
screen -t post_initialization 39 sh -c 'sleep 1; screen -X msgwait 5; screen -X msgminwait 1'

This is a sample per-session configuration file etc/term/emacs.screenrc, which starts one window running Emacs (no longer useful with Emacs 23 which can attach to multiple terminals).

sessionname emacs
source 0.screenrc
screen -t emacs emacs -nw

And this is an adapted version the script I use to invoke these sessions. Call it with e.g. scr emacs to pick up the session shown above. If there is no configuration file corresponding to the session name in ~/etc/screen, I run a shell.

#!/bin/sh
session_name=$1; shift
if [ -r "$HOME/etc/screen/$session_name.screenrc" ]; then
  exec screen -S "$session_name" -d -RR -c "$HOME/etc/screen/$session_name.screenrc" "$@"
else
  if [ $# -eq 0 ]; then set "$SHELL"; fi
  exec screen -S "$session_name" -d -RR -c "$HOME/etc/screen/0.screenrc" "$@"
fi
share|improve this answer
    
Looks very useful . Especially source command. As I stated, I'd like to ensure there is one session for each name. IMHO -d -R will be better in my case, cause I will be warned in case if there will be more than one session with given name. What do you think about it? –  Grzegorz Wierzowiecki Nov 30 '11 at 7:28
    
Are screen -dR name -c conf and screen -S name -d -R -c conf equivalent? –  Grzegorz Wierzowiecki Nov 30 '11 at 7:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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