What exactly is the difference between screen -x and screen -r. I know they're both for reattaching to the screen, but not sure I grasp when to use one or the other.
-r attaches a detached session, while -x attaches an attached session without detaching it, so it runs in both the places at the same time. Another useful option combination is -d -r which detaches a session if needed first and then reattaches it.
Related
I alway work in a screen session with some windows, one for shell, one for mysql, one for music player, one for irc, and so on...
The problem is, when you create a screen session, it only creates one window by default. So I have to do Ctrl-a c then issue commands, again and again.
So I wrote a bash function to do this.
d(){
local i=workspace
screen -qls $i
if [ "$?" -ne 11 ];then
screen -dmS $i
screen -S $i -X screen mysql -uroot -p
screen -S $i -X screen irssi
screen -S $i -X screen nvlc $music -Z
screen -r $i -p0
else
screen -r $i
fi
}
My question is, is there a way to start a new screen session with some windows? By this question, I mean new session, NOT for existing sessions using '-X'. And, screen built-in feature, I mean, no shell scripting involved.
I didn't consider .screenrc file at first, because commands in it will be invoked every time you call screen, but sometimes I need to create a new screen session with different things.
One thing I forgot is that, I can choose configuration file.
So I think answer is:
d(){
screen -d -R -S workspace -c ~/.workspace
}
and content of ~/.workspace should be:
screen
screen mysql -uroot -p
screen irssi
screen nvlc
select 0
When I try to resume a screen session with
$ screen -r name
I get
WriteMessage: Bad file descriptor
How can I fix that? (Ideally being able to resume the screen, but at least remove it if resuming is not possible)
The problem why it occurs might be that the SSH connection was interrupted.
This worked for me:
Find the process's pid by running screen -R
Use screen -r <pid> instead of screen -r <name>
If the screen didn't detach from previous ssh session, you might also need to kill dead sshd process.
I got this error when trying to detach all screens with screen -D. I had attached the screen with
screen -R screen-name-here
To detach, I needed to specify the screen name:
screen -D screen-name-here
I am trying to figure out a way to list all of the tabs in a specific screen session from command line. Specifically, I just want to figure out if a tab exists of some particular name.
I have a script which creates a new tab in a session and runs a script there for a list of tab names. For some reason, there are occasionally one or two tabs that don't get created and this throws off the top level script. I want to add an acknowledgement in my top level script that checks if the particular tab was created and, if not, have a log that tells me this when I go back and look at the data.
Here is my top level code snippet, in case you may have any pointers on why a particular tab would not get created. My guess is that the tabs get created too quickly and this potentially causes an error. There are definitely no name conflicts
for f in $PWD/*; do
if [ -d $f ]; then
CMD="cd $f; bash cmd"
# Creates a new screen window with title '$f' in existing screen session
screen -S $SESSION_NAME -X screen -t $f
# Switch terminal to bash
screen -S $SESSION_NAME -p $f -X stuff "bash$(printf \\r)"
# Launch $CMD in newly created screen window
screen -S $SESSION_NAME -p $f -X stuff "$CMD$(printf \\r)"
fi
done
Thanks for the help!
You can use the -Q parameter with the command windows
-Q Some commands now can be queried from a remote session using this flag, e.g. "screen -Q windows". The commands will send the response to the
stdout of the querying process. If there was an error in the command, then the querying process will exit with a non-zero status.
Using "screen -D -R -S foo", one can attach to an existing session named "foo", or if said session doesn't exist, create it.
How does one also source a file that contains screen commands?
I thought that this would work:
screen -D -R -S foo -X source file
Unfortunately, that fails with this message:
No screen session found.
EDIT: As zebediah49 pointed out in a comment, I left out the "source" in "-X source file" by mistake. Updated now.
OK, from a close reading of the man page I note:
-X Send the specified command to a running screen session. You can
use the -d or -r option to tell screen to look only for attached
or detached screen sessions. Note that this command doesn't work
if the session is password protected.
running screen session. In other words, I don't believe you can do what you're looking for like that, with only one command. However, you can
create the window if it does not exist
send the command to the window
connect to the window:
NL=$'\n'
NAME=foo
screen -ls | grep "$NAME" || screen -d -m -S "$NAME"
screen -r "$NAME" -X stuff "source file$NL"
screen -D -R -S "$NAME"
(Clarification of how -X works, from Send commands to a GNU screen )
I want to make a bash script that echo's something into one of the screens that I have running (screen -r is how I get to it in SSH).
I was wondering how I would make the script execute itself in screen -r?
I basically just want the script to say something on a minecraft server through the console and would set up a cronjob to say it every x minutes.
Cheers,
You can use the -X option of screen to send commands to a running screen session.
Also the -p option is useful in this case, as you can use it to preselect a window
As an example you can run a script in a running screen session on windows 0 via:
screen -p 0 -X stuff './fancy_script.sh^M'
Note, that you have to append the return key-code to execute the script.
You can look in /dev/pts. I don't have screen here to test, but you can echo something to an opened terminal with, for example, echo "toto" > /dev/pts/0 (it will be echoed on the first opened terminal).