Sending commands to the active program in a screen session? - linux

I've got a server running inside a screen session, and I want to send this program a command. I thought screen -X was my answer, but all that gives me access to is screen commands (title, exec, etc).
I need to be able to send the command as if I was typing it into the program. Any ideas?

You may use screen's -p and -X options in conjunction with the exec command.
Try screen -X exec ".\!\!" echo foo, for example, to send "foo" to the currently-running program in the screen.
You might also want to try screen -X exec ".!" echo foo if the first command is not working.

Related

Issue command in screen without attaching

I'm running a Minecraft server, I want to make an sh script that when run, will attach to a screen and issue commands to the server. This is especially useful for long commands that may need multiple other commands to run.
I've tried screen -x zencraft/zencraft bash -c say test. But that didn't work. Neither did screen -x zencraft/zencraft bash -c echo -e "$(say test)" (but I didn't expect that to work anyway, because putting commands in $() in an echo just runs it as bash.)
I'm clueless on what to do at this point.
Note: The screen is shared between users and zencraft owns the screen. This is why using +x zencraft/zencraft is required.
I've figured this out myself, thanks to the help of some people.
screen -S zencraft/zencraft -p 0 -X stuff 'command' works. The stuff command is the key here - it actually does the magic of running the command, from what I know.

How to use bash to open multiple screen and let them run independently

Screen only supports ctrl+a+d for temporary departure, I try to use expect's send "/ 01d" but there is no response, there is no response to executing the expect script alone in screen, is there a better way to complete execution in screen and create the next screen?
I had a lot of scripts to monitor and might need to monitor in real time, so I chose screen to manage them, and now I want to write a script that quickly traverses and runs all the scripts in the directory.
I execute changed.sh directly in screen
change.sh
#!/usr/bin/expect
send "\01d"
According to other people's answers, screen should have generated the Detached event.
full scripts
#!/usr/bin/env bash
#cd /home/centos/Recorder/config
#ls|grep .txt|sed 's/.txt//g'
for ((NUM=$(ls /home/centos/Recorder/config|grep -c .txt); NUM>0; --NUM))
do
NAME=$(ls /home/centos/Recorder/config|grep .txt|sed 's/.txt//g'|sed -n "$NUM"p)
sleep 3
screen -S $NAME /home/centos/Recorder/index.sh $NAME
sleep 3
/usr/bin/expect <<EOF
send "\01"
send "d"
expect eof
EOF
done
use this can solve without ex
screen -dmS $NAME

Is there a way to code Ctrl+a :multiuser on when using screen?

I want to run a script that sets up a screen session and then automatically makes it into a multi-user and also adds one of the users on my system.
So far, I have a script that creates the screen session, but I have to manually make it into a multiuser session then also add the user.
As far as I have seen there is no actual coding to do this and the only way to do it is with the Ctrl+a command.
Does anyone know of a way that means the command can be done in a bash script?
You can automatically run custom commands from a configuration file - by default $HOME/.screenrc will be loaded, if it exists, so you can just do:
echo "multiuser on" >> $HOME/.screenrc
to make your default screen start with :multiuser on. If you want to have a separate config from the default, just save the config with an alternative filename, and start screen with the -c option, e.g.
screen -c multiuser.conf
It is possible to do it without entering the screen, using -X. The following lines (run by Alice) start a script in a screen and add access for the user bob.
screen -S "myscreen" -dm bash script-that-i-like.sh
screen -S "myscreen" -X multiuser on
screen -S "myscreen" -X acladd bob
Bob can then join using:
screen -x alice/myscreen

using screen session in shebang of a bash script

I have recently come across this post which says that it is possible to force a script to run in screen with a shebang. My question is, how does one do this if I want to reconnect to an existing screen session (and thus force the script to run under screen).
So, on my server, when I do screen -list, I see:
There is a screen on:
22566.myscreen (10/26/13 23:47:09) (Detached)
1 Socket in /var/run/screen/S-admin.
Now, I have the following bash script, and I would like the bash script to be run with the above screen session. At the moment, I have something like this:
#!/usr/bin/screen -r "myscreen" /bin/bash
# /home/foo/jobscripts/script.sh
#
echo $STY
git status
touch /home/foo/jobscripts/testsuccess.txt
exit 0
... but this obviously does not work and tells me Error: Unknown option r "myscreen" /bin/bash Wondering if there was a way to specify in shebang to reattach to my above screen.
For Linux, I think we should just use something like pid.sessionname.
For other Unix-like OS, it may also indicate the TTY like this pid.tty.sessionname.
To be sure, consult your manpage of screen : man screen

How to execute a command inside a screen session

I would like to know how to execute a command inside a screen session.
i searched and I found this :
screen -S nameofscreen -X stuff "command"
When I type this, the command is typed inside the screen but it is not executed.
So my queston is how to press enter using this command.
I'd do something like this:
screen -S sessionName bash -c 'cmd; exec bash'
it starts a new session executes cmd and launches shell (otherwise it'd drop that new session).
-X will allow you to send input to a specified session -- that's why your command didn't execute. To execute it you'd need to add enter sign like Paul suggested. It can be done with Ctrl+v and then Enter. That will produce that ^M. So:
screen -S sessionName -X stuff 'cmd^M'
That, in itself, won't however attach a detached session.
In bash, you can use \n in the $'...' construct:
screen -S nameofscreen -X stuff $'command\n'
In the bash shell you can use ctrl-V to explicitly put non-printable characters into a string. So try ctrl-V ctrl-L ctrl-V ctrl-M at the end of your command just before the ".
It took me some time, but what I found is:
Version of screen 4.06 has a bug.
If you want to send a command over a shared screen session like this, it fails:
screen -S shared_session_name -X stuff "command \n"
Screen fails with an error:
Cannot opendir /run/screen/S-$USER: Permissions denied
After update to the version screen 4.09 it works.

Resources