using screen session in shebang of a bash script - linux

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

Related

Automatically starting script in foreground on headless debian

Does anyone know any way of doing this? The help would be greatly appreciated, I've been beating my head over this one for a while now, and I can't seem to find any way to get scripts to auto-start and display in the foreground as they would if I had manually started them.
what i want to do is basically just power on the vm, let it boot, then watch the script run and echo results to the console or whatever the script would normally display if ran manually
I've been able to use Cron and systemd to run the script I would like to run at startup, but I cannot figure out any way to get these scripts to run in the screen, as they would if I had typed ./startup_script.sh
i am currently testing everything in a headless Debian 11 vm. Auto login as root is already enabled. i just need to complete this last step but i don't know how to do.
You can fire up a tmux session and send keys it to start a script like this:
tsid="session_name"
my_script="/home/script.sh"
# create new tmux session
tmux new-session -d -s ${tsid}
# launch the script in the tmux session
echo "launching script in a tmux session called ${tsid}"
tmux send-keys -t ${tsid} "${$my_script}" 'C-m'
I do something similar to start a "quad-pane" to my raspberry pi devices. I can send commands to each pane via ssh without looking at the screen I am sending commands to. I can send commands to the panes from any ssh session without actually connecting to a gui, or being in the "session". This way, another machine can have that session up on a monitoring display and I can momentarily ssh over a command to the host and it will show over there without ever actually connecting to the window. The panes can run scripts and and have the output on the each respective screen/pane waiting for me to connect later and look at it. Every now and then I'll pull up my quad feed to see what's happening... then disconnect and leave it all running.
# function to send an arbitrary command to a tmux pane
# arg1: full id of pane, e.g. "pi4-host01:quad-feed.0"
# arg2+ command(s) to send to pane
tmux_send_command_to_pane_id() {
local tpid cmd
tpid="${1:-0}"
cmd="${#}"
echo "cmd to send to pane: ${tpid}: ${cmd}"
tmux send-keys -t "${tpid}" "${cmd}" 'C-m'
}
If you pair something like this with cron or systemd, i'd imagine you could get to your goal relatively quick. It does depend on installing tmux. I know others use screen, but I have become a fan of tmux for whatever reason.
A nice answer on systemd scripts here -> https://unix.stackexchange.com/a/47715/376937
Here is another question that may help as well:
Tmux command to run shell command on active pane?

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.

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

Unable to run X11 (graphical) programs from atd

I am trying to schedule the execution of a shell-script with the Linux tool "at".
The shell script (video.sh) looks like this:
#!/bin/sh
/usr/bin/vlc /home/x/video.mkv
The "at" command:
at -f /home/x/video.sh -t 201411052225
When the time arrives, nothing happens.
I can execute the shell-script just fine via console or by rightclicking - Execute. VLC starts like it is supposed to. If I change the script to e.g. something simple like
#!/bin/sh
touch something.txt
it works just fine.
Any ideas, why "at" will not properly execute a script that starts a graphical program? How can I make it work?
You're trying to run an X command (a graphical program) at a scheduled time. This will be extremely difficult, and quite fragile, because the script won't have access to the X server.
At the very least, you will need to set DISPLAY to the right value, but even then, I suspect you will have issues with authorisation to use the X screen.
Try setting it to :0.0 and see if that works. But if you're logged out, or the screensaver's on, or any number of other things...
(Also, redirect vlc's stdout and stderr to a file so that you can see what went wrong.)
Your best bet might be to try something like xuserrun.
I suspect that atd is not running. You have to start the atd daemon before (and to set DISPLAY variable like chiastic-security said) ;)
You can test if atd is running with
pidof atd &>/dev/null && echo 'ATD started' || echo >&2 'ATD not started
Your vlc command should be :
DISPLAY=:0 /usr/bin/vlc /home/x/video.mkv
(Default display)

gnome-terminal executes commands from file

I need an example of gnome-terminal command to read lines of text from a file and executes them one by one in different terminal or a tab.
So this would be the process. I would run gnome terminal command and it would read 10 commands from a file. Then it would execute those 10 commands in 10 different tabs/terminals. And of course those tabs/terminals would remain opened. I found this question Avoid gnome-terminal close after script execution?
The third answer from the top is very helpful. I managed to open 1 command from a file. But I need 1 file with 10 command lines to be opened like I wrote above.
Thanks.
I recommend to use screen for this, if that can be acceptable to you.
You could create a commands.screenrc file like this:
screen bash -c 'command1; echo press any key; read'
screen bash -c 'command2; bash'
screen mutt
screen emacs
screen
You can define as many programs as you want. Start screen with:
screen -c commands.screenrc
I don't know what kind of commands you want to run. If you want to see their output, then write like the first example above: execute the command in a bash shell, which will "pause" after the command was executed. Or the second line, which, after running the command will start another bash shell. Otherwise the screen window would exit automatically.
If you are not familiar with screen, you will need to learn some basic key strokes to get around, and to be able to switch between windows. The first few pages of this presentation should be enough to get you started.

Resources