Issue command in screen without attaching - linux

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.

Related

Start detached screen with ssh , without killing after the commands execute [duplicate]

I'm lazy, and I prefer that computers do my work for me. I ssh into several machines on a daily basis, so I created a simple script that launches some xterm windows and places them in positions I want (as you can see, I'm using bash):
#!/bin/bash
xterm -geometry 80x27+1930+0 &
xterm -geometry 80x27+2753+0 &
xterm -geometry 80x27+1930+626 &
xterm -geometry 80x27+2753+626 &
However, the next thing I do is go to the first window and type in
ssh server_a
then in the second
ssh server_b
and so on. What I'd like to do is have my script do the ssh commands in each xterm window, and then leave the windows open for me to do my work. I've seen the -e option for xterm, but the window closes after I execute my command. Is there a way to do this?
I apologize if this is a duplicate question. I've searched around and haven't had any luck with this. Many thanks!
I'd love to see a more elegant answer, but what I came up with does work:
xterm -e bash -c 'echo foo; exec bash'
Replace echo foo with the command of your choice, and you're good to go.
This answer gives one of the best answers I've seen so far to do this. Use the bash --init-file flag either in the shebang or when executing the terminal:
#!/bin/bash --init-file
commands to run
... and execute it as:
xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang
My only real complaint with the exec option is if the command executed prior to exec bash is long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.
Another option is cmdtool from the OpenWin project:
/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash
... where cmdtool injects the commands passed with -I to the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.
Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.
Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window
See: https://superuser.com/a/610048
"ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection"
https://github.com/duncs/clusterssh/wiki
$ cssh server_a server_b
$ command

Writing a Script to execute commands?

I have never written a script, so bear with me. What I need to do, is make two scripts that I can click on from the desktop, will both open their own terminal (And stay open until I manually close it) and run the given lines.
For the first one, I have to manually run this:
cd home/pi/PiBits/ServoBlaster/user
sudo ./servod
For the second:
cd ~/scratchClient
python crs/scratchClient.py -c servoblaster
How would I do this? I read a few things about putting xterm -e and such in front of it, but none of that works for me...
By the way, this will be used on Raspbian Linux.
EDIT, this worked for me:
Link: ubuntuforums.org/showthread.php?t=1336228 The line that was used: gnome-terminal --execute bash -c "/path/scriptname ; bash"
You just need to add a shebang, which means putting this in the first line of the script:
#!/bin/sh
This causes the bourne shell to be used to interpret the script, this is (probably) the same interpreter that runs when you are in your terminal. Then you should make the script executable chmod +x <script>
Try this.
xterm -hold -e 'cd /home/pi/PiBits/ServoBlaster/user
sudo ./servod' &
and
xterm -hold -e 'cd /home/pi/scratchClient
python crs/scratchClient.py -c servoblaster' &
If it doesn't work, perhaps you should explain in what way this fails. If it works, you can add a shebang in front, save them in files, chmod +x those files, and click away to your heart's content (or perhaps acquire a more sophisticated taste where you simply run these as background jobs without any xterm or other anxious GUI).
Solution was to use gnome-terminal... Found an UbuntuForums post with a similar question such as mine.
gnome-terminal --execute bash -c "/path/scriptname ; bash"

How can I launch multiple xterm windows and run a command on each, leaving each window open afterward?

I'm lazy, and I prefer that computers do my work for me. I ssh into several machines on a daily basis, so I created a simple script that launches some xterm windows and places them in positions I want (as you can see, I'm using bash):
#!/bin/bash
xterm -geometry 80x27+1930+0 &
xterm -geometry 80x27+2753+0 &
xterm -geometry 80x27+1930+626 &
xterm -geometry 80x27+2753+626 &
However, the next thing I do is go to the first window and type in
ssh server_a
then in the second
ssh server_b
and so on. What I'd like to do is have my script do the ssh commands in each xterm window, and then leave the windows open for me to do my work. I've seen the -e option for xterm, but the window closes after I execute my command. Is there a way to do this?
I apologize if this is a duplicate question. I've searched around and haven't had any luck with this. Many thanks!
I'd love to see a more elegant answer, but what I came up with does work:
xterm -e bash -c 'echo foo; exec bash'
Replace echo foo with the command of your choice, and you're good to go.
This answer gives one of the best answers I've seen so far to do this. Use the bash --init-file flag either in the shebang or when executing the terminal:
#!/bin/bash --init-file
commands to run
... and execute it as:
xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang
My only real complaint with the exec option is if the command executed prior to exec bash is long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.
Another option is cmdtool from the OpenWin project:
/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash
... where cmdtool injects the commands passed with -I to the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.
Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.
Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window
See: https://superuser.com/a/610048
"ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection"
https://github.com/duncs/clusterssh/wiki
$ cssh server_a server_b
$ command

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

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.

Resources