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
Best Answer
Instead of
ps aux | grep SCREEN | sed -n 1p | awk '{ print $2; }'
, you could just doOr, even more recommended, if you install
procps
:(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:
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
which is then reattached with