How to execute a command inside a screen session - linux

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.

Related

passing the command with an enter in existing screen session using bash script

I am writing a bash script.
Below is my command
screen -x stack -p n-api -X stuff "ls"
This command just pastes this command never executes.
Hence i used
control + V + enter command
to form a symbol
^M
which is essentially an enter. but whenever i open the script to edit some other pieces the command goes away and again i am not able to press enter in the screen session using bash script
From here, this seems to work for me:
screen -x stack -p n-api -X stuff "ls$(printf \\r)"
Other solutions that can be found in How to execute a command inside a screen session are
screen -x stack -p n-api -X stuff 'ls\n'
as well as your solution by inserting ^M with ctrl-V ctrl-L ctrl-V ctrl-M

Shell script: Excecute a command in "screen"

I use a USB-Dongle on my linux-server (raspberry).
To open the conecction and use the firmware, to do this, I have to open a terminal with "screen".
screen /dev/ttyACM0
There is no problem to open this connection with a shell script.
My problem is to execute a command in this screen.
The firmware has some command references for example V to get back the version number.
First of all, I tried a pipe (|)
screen /dev/ttyACM0 | echo "V"
But, this have no correct result.
Someone tell me I can use the screen command -X to execute something.
screen -T CUL /dev/ttyACM0
screen -X V
This seems to work.
The problem: "screen" gives an error message: -X: unknow command "V"
When I open the screen and entered the coammand V I get an output.
Some ideas?
Try this command:
screen -X stuff 'V'$(printf \\r)
(you also need the carriage return)

How to reset terminal without losing the current command?

I'm trying to create a keyboard shortcut to reset the current terminal.
I'm using a .inputrc entry like this:
"\C-K": 'echo -en "\\033c"\n'
It works, however, I can't do that while typing a command.
For instance, if I'm typing a command like this (with the cursor at the end):
$ foobar
and press CTRL+K, it will become
$ foobarecho -en "\033c""
and, of course, it is not going to work.
It is possible to do it?
I suppose it is possible, since that's what CTRL+L does.
The only problem with CTRL+L is that it won't clear the entire terminal, including history, just what's on screen.
I am not sure how to do that in inputrc, but you can do it with the bind command and its -x option.
bind -x '"\C-K": "echo -en \\033c"'
You can put the above line in your .bashrc and it will offer the same behaviour as the one you describe with CTRL+L.
do not understand "reset the current terminal". Clean your terminal? If so you can use command clear

Sending commands to the active program in a screen session?

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.

How can I make GNU Screen start a new window at the CURRENT working directory?

By default, when you create a new window in GNU Screen, it will start in the directory where Screen is invoked. I want to start a new window in GNU Screen at the current working directory of the window I'm currently in. How can I do that?
See the GNU Screen chdir command. All new windows created in Screen use this as their initial directory. Using this, you can do something like:
chdir /home/dan/newscreendir
screen
And your new window (along with any future created windows) will be in the set directory. If it's always going to be the current working directory you may be able to set something up in your screenrc to do this one in one command.
See the GNU Screen man page. It's quite comprehensive.
Screen chdir command
Screen cannot access your shell variable nor execute backticked commands. The closest I can get to doing it in one click is with a small Bash script like this:
screen -X setenv currentdir `pwd`
screen -X eval 'chdir $currentdir' screen
Or more compactly:
screen -X eval "chdir $PWD"
screen -X sends the command to the currently running Screen session. The first line creates a variable called currentdir. The second line sends the currentdir to the chdir command and then creates a new Screen window.
The simple solution is to put the following strings in your ~/.screenrc file and then use Ctrl + X to open new windows:
bind ^x
bind ^x stuff "screen -X chdir \$PWD;screen^M"
http://www.michaelkelleher.info had more tips for intermediate/advanced screen users, but since that site seems to have gone away, you can find the archive of it in Michael Kelleher's Personal Website on Archive.org.
I didn't find any solution that would work when you already had a process running in a window, so I came up with my own idea. I added following lines to my .bash_profile file:
scr_cd()
{
cd $1
screen -X chdir $PWD
}
if [ "$TERM" == 'screen' ]; then
alias cd=scr_cd
fi
The screen's working directory is updated every time you change a directory. Someone may not like this approach, but it works like a charm.
Perhaps this is specific to Byobu, but simply typing screen opens a new window in the current directory.
To make Screen open a new tab/window in the current directory, you can add the following code to your .screenrc file:
bind c stuff "screen bash^M"
This will cause the Ctrl + a c command to open new tabs/windows in the directory of the current window/tab.
Note: You must ensure that Screen does not start a login shell by default, because that will cause the shell start in the default directory for a login shell rather than the current directory. This means that in your .screenrc file, your shell command cannot include a dash ('-') character.
For example, this is wrong (i.e., it will start a login shell):
shell -$SHELL
But this is right (i.e., it will not start a login shell):
shell $SHELL
Note 2: Unfortunately, this method does not behave exactly like the default new window/tab command in Screen. Instead, it writes the command to the current window and executes it to create the new window/tab, so it will not work during some long running shell process. In other words, this keyboard shortcut can only be executed whenever normal shell commands can be executed.
Note 3: If you want Screen to open new windows/tabs in the current directory and open a login shell, you can add the following code to your .screenrc file:
bind c stuff "screen bash -l^M"
You could also run:
screen -X eval "chdir $(pwd)"
Or if you want to start a new window as soon as you set chdir, use:
screen -X eval "chdir $(pwd)" screen
I have a nearly perfect solution for Bash. :)
If you never use password to set a lockscreen password, just add this to file $HOME/.bash_profile:
export PROMPT_COMMAND='screen -p $WINDOW -X chdir "$PWD"'
Do you need a password? With this:
# The digest of password "abc" is ID1wIq4l2t7s6
export PROMPT_COMMAND='screen -p $WINDOW -X eval "password none" "chdir \"$PWD\"" "idle 0 password ID1wIq4l2t7s6"'
I just hope the developers of Screen add the environment variable PWD as soon as possible.
In your .screenrc file, add a line that uses the chdir command if you want the same one every time.
If you have a running Screen session inside that session, you can type:
screen -X chdir [argument]
Without an argument it will be your home directory, the same result as typing cd.
If you have a script (this is a programming Q&A site) or are outside Screen and Screen is running, you can issue:
`which screen` -x -X chdir [argument]
Which you'll likely follow with running some new process in Screen with:
`which screen` -x -X screen [command to run in that directory] [arguments for the command]

Resources