Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I run a Minecraft server on my Arch Linux laptop. The server software for Minecraft runs as an interactive program on a command line, so I start it in a detached screen session, allowing me to reattach every time I need to access it.

To do this I used a script (I don't use screen any other time, so don't worry about "grep SCREEN")

#!/bin/bash

PID=ps aux | grep SCREEN | sed -n 1p | awk '{ print $2; }'

sudo screen -r $PID

This will get the PID of the screen session which the server is running in and then reattach to that screen.

Now I'm wondering, as I can't find this anywhere, if I can use a one-liner to redirect the output of

ps aux | grep SCREEN | sed -n 1p | awk '{ print $2; }'

directly to $argument in

sudo screen -r $argument

without needing to span 2 lines

Basically I want to redirect the stdout of awk into an argument of screen, instead of as the stdin of screen.

This seems like it should be relatively easy to find and do, but I'm having a lot of trouble finding anything about it on Google.

Any help is appreciated! Thank you

share|improve this question
    
The answers to this question should work. –  Bob Jun 3 '12 at 13:34
add comment

3 Answers

up vote 6 down vote accepted

Instead of ps aux | grep SCREEN | sed -n 1p | awk '{ print $2; }', you could just do

ps aux | awk '/SCREEN/{print $2;exit}'

Or, even more recommended, if you install procps:

pgrep SCREEN

(Wrap these commands in backticks as explained, or use the $() construction which is easier to read and nest, and does the same thing.)

Or, even more recommended: if you only have a single Screen session running, simply:

screen -r

will by default attach to the only existing session.


As noted in a comment: if you perhaps want to run multiple Screen sessions in the future, use the session naming ability. Start a named session with

screen -S minecraft

which is then reattached with

screen -r minecraft
share|improve this answer
1  
I almost died when I read your last point, this is why I should read man pages...also why I need to read up more on text manipulation programs –  Joe Bentley Jun 3 '12 at 15:30
1  
Great answer! I think giving the screen session a meaningful name would be even better, in case OP decides to actually start using screen in the future. The screen name is given when the screen is first started, then it can be used to reattach like: screen -r <NAME> (without the chevrons). –  orryowr Jun 3 '12 at 17:43
    
@ryanrhee90: Very true, I add that remark to my post. –  Daniel Andersson Jun 4 '12 at 6:52
1  
@JoeBentley: This is a good answer for your specific problem with screen. However, if one looks at the title of your question, only the two other answers are generically applyable. –  DevSolar Jun 4 '12 at 7:30
    
@DevSolar: Both yes and no; the first part describes shorter ways of getting the PID, and backticks and $() is mentioned. But yes: it is an example of the true question not really being what is asked, so it would be better to either reformulate the question (the subject would probably suffice) to specifically concern Screen (the other answers should then still be sequitur), or remove the Screen references and make it a Bash question. But the latter approach would actually not be what the questioner was after, and it would end up being somewhat of a duplicate of the linked post. –  Daniel Andersson Jun 4 '12 at 8:36
add comment

xargs will do that

$echo abc | xargs prog

In the above, xargs will execute prog with arg abc i.e. $prog abc

Another example

$ echo abc | xargs echo w x
w x abc
share|improve this answer
    
Also a good answer! Thank you very much I went for the other suggestion as it was slightly simpler to do –  Joe Bentley Jun 3 '12 at 13:45
add comment

Assuming you're on bash, another way is to use backticks. AFAIK, backticks are evaluated first and the result is used in the rest of the expression.

sudo screen -r `ps aux | grep SCREEN | sed -n 1p | awk '{ print $2; }'`

I think that should do what you want.

As another commenter pointed out, you can use xargs as well, but I like to reserve that one for multi-argument stuff. I feel that backticks are cleaner for what you want to accomplish.

share|improve this answer
    
Very nice thank you, although the ps should be in the backticks too! –  Joe Bentley Jun 3 '12 at 13:45
1  
Good catch, I've fixed the answer. :) Thanks. –  orryowr Jun 3 '12 at 13:53
1  
You can also use $(prgm), if you want to nest them or something (Heaven help the maintainer...) –  Christian Mann Jun 3 '12 at 16:38
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.