You are not logged in.

#1 2009-06-30 16:56:07

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

[SOLVED] Screen help

I'm trying to get ncmpcpp to launch from a keyboard shortcut (openbox) and open up either in a new screen session/terminal if there isn't a terminal/screen open already or in an existing session but in a new screen window in that session. I'm afraid I can't quite get it right ... I can get it to open in an existing screen session, but it won't open urxtv if the session is detached and it won't start a new session if one is not already running. Sorry that sounds confusing!!

I use this to open my normal terminal sessions:

<keybind key="W-z">
      <action name="Execute">
        <command>urxvtc -geometry 120x40 -e screen -d -R -t urxvt -S terminal</command>
      </action>
    </keybind>

And I've got this to open ncmpcpp as described above:

<keybind key="W-m">
      <action name="Execute">
        <execute>screen -X screen -t ncmpcpp ncmpcpp</execute>
      </action>
    </keybind>

I've tried the various combinations of -d -R, -D -R, -D -RR, but none will seem to open a new window as well.

Any screen guru's care to take a crack at this?

Thanks!
Scott

Last edited by firecat53 (2009-07-02 16:37:43)

Offline

#2 2009-06-30 20:42:44

plurt
Member
Registered: 2008-10-16
Posts: 88

Re: [SOLVED] Screen help

maybe make a script that does exactly what you want and make a shortcut for that?

Last edited by plurt (2009-06-30 20:47:41)


When everything's coming your way, you're in the wrong lane I say.
FAQ / Beginners Guide / The Arch Way

Offline

#3 2009-06-30 21:04:37

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Screen help

i'm only posting this because you're not getting much other help... this may or may not be useful.

i'd suggest you write a short shell script that does what you need... i don't know the screen commands well enough but i can give you some psuedo-code to get started...

#!/bin/bash

# open a new terminal with screen and ncmpcpp
open_new() {
  urxvtc -geometry 120x40 -e screen -t ncmpcpp ncmpcpp &
  exit 0
}

# open ncmpcpp in a running attached screen
# tty/pts number passed in as $1
# this may need tweaking...
open_in_existing() {
  screen -X $1 screen -t ncmpcpp ncmpcpp &
  exit 0
}

# reattach a detached screen and open ncmpcpp in it
# tty/pid number is pass in as $1
# can you use -R and -t here?
open_reattach() {
  urxvtc -geometry 120x40 -e screen -R $1 -t ncmpcpp ncmpcpp &
  exit 0
}

attached=$(screen ls | awk '/Attached/ {print $1}' | head -n1 | cut -d '.' -f 1)
detached=$(screen ls | awk '/Detached/ {print $1}' | head -n1 | cut -d '.' -f 1)

# if an attached is found...
[ -n $attached ] && open_in_existing $attached

# if a detached is found...
[ -n $detached ] && open_reattach $detached

# if nothing's found...
open_new

this doesn't do exactly what you want, it's just to get you started.  what it does do:

looks at screen -ls to find all running screens

it will find the first Attached screen and /should/ open ncmpcpp in that screen
if none are found, it then looks for the first Detached screen and /should/ reattach it in a new urxvtc and open ncmpcpp in that screen
if none are found, it /should/ open a new urxvtc with screen and ncmpcpp in that screen

i can't really test all this so i'm kinda shootin from the hip here... let me know how it goes.  i tried to structure it so it can be easily changed/extended to suit your needs, hopefully it's readable.

Offline

#4 2009-06-30 23:48:41

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [SOLVED] Screen help

Thanks! I was hoping it could be done with just screen's builtin commands, but I'll give the script a try as soon as I can sit down for a few minutes.

Scott

Offline

#5 2009-07-01 17:24:14

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [SOLVED] Screen help

Ok, I've massaged the script a little bit, but I'm having trouble getting the urxvt window to stay open when it adds ncmcpp to an existing, detached session.

If I open (or use gmrun) a non-screen urxvt window -- just to run commands from --, and enter

urxvtc -e screen -d -R -X -S terminal screen -t ncmpcpp ncmpcpp

This will open ncmpcpp in a new screen window in the existing detached session. However, the urxvt window just flashes up and then closes right away. I can't get any combination of commands to keep that window open. I tried the -hold option for urxvtc, but that just holds open a blank urxvt window. Ncmpcpp still gets added to the existing screen session, but it doesn't open up and stay open (visible).

So close!

Thanks!
Scott

Here's the script as it stands -

#!/bin/sh

# open a new terminal with screen and ncmpcpp
open_new() {
  urxvtc -geometry 120x40 -e screen ncmpcpp &
  exit 0
}

# open ncmpcpp in a running attached screen
# tty/pts number passed in as $1
# this may need tweaking...
open_in_existing() {
  screen -X -S terminal screen -t ncmpcpp ncmpcpp &
  exit 0
}

# reattach a detached screen and open ncmpcpp in it
# tty/pid number is pass in as $1
# can you use -R and -t here?
open_reattach() {
  urxvtc -geometry 120x40 -e screen -X -S terminal -t ncmpcpp screen ncmpcpp &
  exit 0
}

attached=$(screen -ls | awk '/Attached/ {print $1}' | head -n1 | cut -d '.' -f 1)
detached=$(screen -ls | awk '/Detached/ {print $1}' | head -n1 | cut -d '.' -f 1)
# if an attached is found...
if [ -n "$attached" ]; then 
    open_in_existing $attached
# if a detached is found...
elif [ -n "$detached" ];then 
    open_reattach $detached
# if nothing's found...
else open_new
fi

Last edited by firecat53 (2009-07-01 22:05:18)

Offline

#6 2009-07-02 14:43:10

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Screen help

well it looks like you moved some things around from the original and now you've got some redundancy.  i put the exit 0's in the functions themselves so the script will end when any one function is executed, thereby negating the need for elif's and you can just run tests in sequence and it'll die when one succeeds...  also i was using the awk line to not only check for an attached or detached session, but also return the tty number so you could pass that to the functions and tell screen exactly what session to act on.  but... it was simply grabbing the first one anyway and it looks like screen's got that built into the options you're using anyway so that can go...

here's a simpler version, with a slightly different approach on the reattach:

#!/bin/sh

# open a new terminal with screen and ncmpcpp
open_new() {
  urxvtc -geometry 120x40 -e screen -t ncmpcpp ncmpcpp &
  exit 0
}

# open ncmpcpp in a running attached screen
open_in_existing() {
  screen -X screen -t ncmpcpp ncmpcpp &
  exit 0
}

# reattach a detached screen in a new terminal
reattach() {
  urxvtc -geometry 120x40 -e screen -d -R &
}

# if an attached is found, this will open there and quit the script
screen -ls | grep -q Attached && open_in_existing

# if a dettached is found, this will attach it, open there, and quit the script
screen -ls | grep -q Detached && {
  reattach
  sleep 2 && open_in_existing
}

# if we haven't quit yet we need to make a new one and quit the script
open_new

maybe this'll work?  i really wish i were home to test myself.

Offline

#7 2009-07-02 16:37:20

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [SOLVED] Screen help

You nailed it! Worked out of the box first try for all three cases smile My scripting is pretty rudimentary, so I was changing things in the original script back to a simplified form so I could troubleshoot it easier....now I've got a little research to do to figure out how this one works and the other one didn't.

Thanks very much!!
Scott

Offline

#8 2009-07-02 17:57:50

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Screen help

great! glad it worked.  i think we failed earlier trying to reattach and send the new ncmpcpp command in one shot.  leave it to unix, one tool one job.  reattach the screen, _then_ send the ncmpcpp command.  i may have to start using this script myself...

Offline

#9 2009-07-02 18:39:46

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [SOLVED] Screen help

Yep, the script would work great for any console program. The only time it doesn't work is if you have multiple screen sessions running...which I typically don't...which is I think what you tried to account with the first iteration of the script.

Thanks again!
Scott

Offline

Board footer

Powered by FluxBB