Using screen in startup script - linux

I'm trying to write a startup script to launch rtorrent. I've planed on using screen in the script. I tried this command in the startup script :
su -l nico -c "screen -dmS rtd rtorrent"
But it seems like it doesn't work (i can't get the screen session back) :
screen -ls
No Sockets found in /var/run/screen/S-nico.
Do i miss something ?

The list of active screen sessions is by default maintained independently per user.
In your screen launch command, the session was started by user nico, so if you want to inquiry about it you should probably run su -l nico -c 'screen -ls' instead.

Related

Minecraft Linux Server | Start.sh problem

I have a Minecraft Server running on Linux.
I use to start the server, a start.sh file with following content:
(This content starts a screen session and the minecraft server)
screen -S {ScreenSession} java -Xmx2G -Xms2G -jar spigot-1.18.1.jar
If I use /restart ingame, the screen session will end and the server won't start. So I have to go into the Linux Server and start the Minecraft Server again.
My question:
How can I make it so, if I use /restart that the server will restart with a active screen session.
If have tried many things.
I hope someone can help me,
~Kitty Cat Craft
There is multiple way to achieve what you want.
If you have lot of servers, you can use a quick bash script with an auto restart like that:
#!/bin/sh
while true
do
java -Xmx2G -Xms2G -jar spigot-1.18.1.jar --nogui
sleep 5
done
When you will stop, it will wait 5 seconds then restart.
With this, you can use: screen -dmS <screenName> sh myScript.sh which will run the script into another screen. It's usefull when you run it from a script which run lot of server, like that:
screen -dmS srv1 sh srv1.sh
screen -dmS srv2 sh srv2.sh
screen -dmS srv3 sh srv3.sh
You can also, if you have only one server, just firstly use screen -S screenName. Then, when you are in the screen, run the script that restart automatically (the script that I gave at first).
Also, prefer use /stop than /restart, because spigot will try to find the script. And if it success, it will run a second time the same script, and so will have ghost process.

"Must be connected to a terminal error" with screen -x command on a Linux Container

I am using Linux containers with Proxmox 4.
When I am connected on an Ubuntu 16.04 container with:
pct enter <id>
I run a script with screen and try to watch the process running with:
screen -x <processname>
I got the error:
Must be connected to a terminal error
When I connect with:
ssh -t <user>#<container_ip>
It works, I can attach the display to the screen.
How can I use screen -x when I am connected with pct enter ?
I found a solution that works for same case(https://www.svennd.be/screen-in-lxc-attach)
Execute this
script /dev/null
Then you can run screen, et voila
This is happening mostly because of the reason that you must have
switched user
to the current terminal user using the command sudo su or user changing command.
You will need to own the shell on which you are working.
You can do so by running this:
script /dev/null.
Alternatively,
You can restart the terminal session with the user(which was used when creating screen) and not coming to current user using
switch user commands.
To know active screens :
screen -list
Alternatively, You can start the session within "hohup" and send to background

How to write bash script to re-attach to a existing linux screen?

I want to write a bash script to re-attach to the existing linux screen, perform some commands and then detach from that screen. I know to create a new linux screen in detached mode and perform some commands. But I haven't found a way to re-attach to a existing linux screen.
You can give a name to a screen session by using the -S option.
Example : screen -dS NAME <command>
Then you can reattach to that session using screen -r NAME
try this;
user#host:/screen -ls
There are screens on:
29229558.pts-5.host (Detached)
46661728.pts-22.host (Detached)
user#host:/screen -r 46661728.pts-22.host
If you see (attached) as below;
user#host:/screen -ls
There are screens on:
29229558.pts-5.host (Detached)
46661728.pts-22.host (Atached)
screen -D -r 46661728.pts-22.host
To re-attach to the screen you have to follow this steps:
Before de-attaching from the screen, execute:
STDOUTTERM=$(who -m|awk '{print $2}')
Then you can attach stdout to whatever you want. When you finish, to reattach to stdout, execute this:
exec > /dev/$STDOUTTERM
You can send a command to a running screen using screen -X exec. See the man page for redirection options.
screen -X exec ls
Alternatively you can send text to a running screen's stdin buffer using screen -X paste, as if the user had typed it. This can be a way of running commands inside an interactive shell.
screen -X register a "ls\n"
screen -X paste a

No Sockets found: screen script called on startup

I have added a script called screen_tronserver.sh to the /etc/init.d/ directory. I have set the permissions so it is executable and called
update-rc.d /etc/init.d/screen_tronserver.sh defaults 100
to add the script to startup.
Inside the file is the following:
#!/bin/bash
echo "creating screen and starting tron server..."
screen -S tronserver -m /home/pi/programming/tronserver/a.out
The a.out file is a compiled program which runs a server I have created. Now when I call
screen -ls
It says no sockets found. However, I have tried connecting to the server and it is successful, indicating the screen/socket is there somewhere, running in the background.
So why can I not see this mystical socket!?
As Andrew is already alluding to, init scripts are run as root, and screens belong to different users. When you type screen -ls you will see the screens of the current user, not all screens on the system.
So yes, sudo screen -ls or sudo screen -r will do the trick.
Edit (for completeness and future reference): as pointed out by the OP, it is necessary to also add the -d flag to the screen command to detach right away in the init script, or else the screen will remain attached.

Kill Attached Screen in Linux

I created a screen "myscreen" in linux and it stopped responding abruptly. I closed the terminal and tried to reopen it. "screen -ls" shows that the screen is Attached. I tried the following commands but nothing responds.
screen -r myscreen
screen -D myscreen
screen -D -RR myscreen
screen -X -S myscreen quit
Any help to reattach to the screen or to kill the screen is very much appreciated.
screen -X -S SCREENID kill
alternatively, you can use the following command
screen -S SCREENNAME -p 0 -X quit
You can view the list of the screen sessions by executing screen -ls
Create screen from Terminal:
screen -S <screen_name>
To see list of screens:
<screen -ls> or <screen -list>
To go to particular screen:
<screen -x screen_name>
<screen -r screen_name>
Inside screen
To Terminate screen:
give ctrl+d screen will get terminated
To Detach screen:
give <ctrl+ad>or <screen -d >screen will get detached
To reattach screen:
screen -x <screen_name> or screen -r <screen_name>
To kill a screen from Terminal:
<screen -X -S screen_name quit>
or
<screen -X -S screen_name kill>
You can use screen_name or process_id to execute commands.
This worked for me very well. Get the screen id via:
screen -r
or
screen -ls
then kill the screen: kill -9 <screenID>
it now becomes a dead screen,
then wipe it out with: screen -wipe
To kill a detached screen use this from the terminal:
screen -X -S "SCEEN_NAME" quit
If you are attached, then use (from the terminal and inside the screen):
exit
From Screen User's Manual ;
screen -d -r "screenName"
Reattach a session and if necessary detach it first
You could create a function to kill all existing sessions. take a look at Kill all detached screen sessions
to list all active sessions use
screen -r
when listed, select with your mouse the session you are interested in and paste it. like this
screen -r
Suppose your screen id has a pattern. Then you can use the following code to kill all the attached screen at once.
result=$(screen -ls | grep 'pattern_of_screen_id' -o)
for i in $result;
do
`screen -X -S $i quit`;
done
i usually don't name my screen instances, so this might not be useful, but did you try screen -r without the 'myscreen' part? usually for me, screen -r will show the PIDs of each screen then i can reattach with screen -d -r <PID>
You can find the process id of the attached running screen.
I found it same as the session id which you can get by command:
screen -ls
And you can use following command to kill that process:
kill [sessionId] or sudo kill [sessionId]
None of the screen commands were killing or reattaching the screen for me. Any screen command would just hang. I found another approach.
Each running screen has a file associated with it in:
/var/run/screen/S-{user_name}
The files in that folder will match the names of the screens when running the screen -list. If you delete the file, it kills the associated running screen (detached or attached).
For result find: Click Here
Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows.

Resources