How do I pass a command to a screen session? - node.js

I'm writing a Linux shell script in which I need to start a new screen session, run a node.js server in the screen, then detach from the screen so that my server runs in the background.
Right now, these are the commands I run manually to do this:
screen
node server.js
[detach screen]
However, I need a way to automate this via the script, and if I just use the above commands in a shell script, it creates the screen and gets stuck there. How can I pass the "node server.js" command to the screen command?
EDIT:
Based on the suggested answer I have a script that works, except that I need to manually create a screen and detach from it before I run it. I tried adding screen -d -m as the first line to create a detached screen, but the script hangs after that line.
tempfile=$(mktemp)
indices=`tail -1 debug.log`
cat > $tempfile <<EOF
node server $indices
EOF
screen -X readbuf $tempfile
screen -X paste .
rm -f $tempfile
How can I create and detach a screen with the script?
This didn't work either:
screen
screen -d

It is as simple as this:
screen -md node server.js
This requires the command to run as a service (as it does), otherwise the screen stops immediately.
To optionally also set a name for the session (e.g. "session-name"):
screen -mdS session-name node server.js
You can then attach to the screen with:
screen -rd session-name
If you want to redirect all output to a file, you can do like this:
screen -mdS session-name bash -c 'node server.js &> output.log'
You can then monitor the output with e.g.:
tail -f output.log
You can list your running screens with:
screen -ls
or
screen -list
Example: Start a Python3 web server in a detached screen
Start a Python3 web server listening on port 8000 that serves files in the current directory, in a named
detached screen:
screen -mdS my-web-server python3 -mhttp.server
Or, with logging to a file:
screen -mdS my-web-server bash -c 'python3 -mhttp.server &> output.log'
For Python 2.x it looks like this:
screen -mdS my-web-server bash -c 'python -mSimpleHTTPServer &> output.log'

EDIT: Try this:
tempfile=$(mktemp)
cat > $tempfile <<EOF
node server.js
EOF
screen -S SessionName -X readbuf $tempfile; screen -RdS SessionName
screen -X paste .
rm -f $tempfile
It should create the temp file, create a screen called SessionName and runs the commands, then delete the temp file. Let me know if that works.

Related

Startup script to run detachted screen, switch user, and run multiple bash commands

I want to create a small startup script that does multiple things in a row in a screen.
The script starts a named, detached screen (screen -S discordbot -d -m works)
The user inside the script is changed (Neither screen -S discordbot -X "su discordbot", screen -S discordbot -X su discordbot, nor screen -S discordbot -d -m bash -c "su discordbot;" seems to work, or at least subsqeuent commands are not executed).
A cd folder change is exectuded.
A java jar or other script is started.
As I run multiple bots, the script needs to be able to do this in slight variation multiple times in a row. Any pointers on how this could be done?
The screen session that you start up will exit as soon as the process that you started exits.
This works, for instance:
$ screen -S discordbot -d -m bash
$ screen -ls
There is a screen on:
2948.discordbot (Detached)
1 Socket in <...>
As does this:
$ screen -S discordbot -d -m bin/discordbot.sh
Where bin/discordbot.sh looks like this:
#!/bin/sh
echo "Sleeping..."
sleep 10
/bin/echo -n "Hit enter to finish this script: "
read
The last two lines to prevent the screen from exiting prematurely. The other various things you want to do within that startup script should also work, assuming that you do this as root so that the su will work without prompting.

Creating new 'screen' within existing screen

I have a game server. It runs on linux via dotnet. I run this inside a 'screen' session.
However I am struggling with restarting this server.
From outside, it is easy. I just kill existing screen via name and create new one.
However when I want to restart server from inside (existing game process starts new process that runs script that kills named screen and starts new one)
To be specific:
stop.sh:
screen -r gameserver -X quit
start.sh:
screen -L -A -m -d -S gameserver /usr/bin/dotnet /gameserver/game.dll
restart.sh:
/gameserver/stop.sh
/gameserver/start.sh
Now If I run restart.sh programmatically from inside screen, it calls stop.sh, which terminates current screen and also this restart.sh script, so the new one is not started.
I tried to run restart.sh via screen:
screen -L -m -d /bin/bash -c /gameserver/restart.sh
But it still doesn't work...
I would expect this to run restart.sh in new screen, where the 'gameserver' screen will terminate and start new one and after that screen running restart.sh will stop. But no :(
Any ideas?
Cant you just kill this process inside the screen?
Something like
screen -r gameserver kill $(ps aux|grep 'game.dll'|awk '{print $2}') ?
I think you probably should NOT run such a script inside screen. Kill the whole session then re-create it inside screen is kind of weird.
And in your script stop.sh, though using -r with -X may sometime work, it doesn't make much sense. I think you should use -S instead of -r.
Run outside screen:
If you replace stop.sh with screen -S gameserver -X quit, and run restart.sh outside screen, it should work.
Run inside screen:
However, if you really need to run it inside screen, you could try to kill a window instead of terminate the whole session.
stop.sh:
# Create a new window before kill 'server'
# Without this, the session will be terminated if 'server' is the only window
screen
screen -p server -X kill
start.sh:
# Are we in the 'gameserver' session?
if [ "${STY#*.}" = gameserver ]; then
# Create a window named 'server' and run your program in it
screen -t server /usr/bin/dotnet /gameserver/game.dll
else
# Create a new screen session named 'gameserver'
# And it's default window is named 'server'
# -T $TERM to make sure things such as '-t' work
screen -L -A -m -d -S gameserver -t server -T $TERM /usr/bin/dotnet /gameserver/game.dll
fi

Screen not starting from rc.local

I have a problem where I am unable to start a screen session at boot using the rc.local file. The specific screen I am starting is for a spigot minecraft server.
This is my rc.local file:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/home/pi/Documents/bootlog.sh
/home/spigot1_12/startspigot.sh
exit 0
This is the startspigot.sh script (with chmod u+x):
#!/bin/bash
cd /home/spigot1_12
boot=$(date)
echo "Starting spigot server in screen \"minecraft\" # $boot " >> /home/pi/Documents/minecraftlog
screen -S minecraft java -Xms512M -Xmx1008M -jar /home/spigot1_12/spigot-1.12.jar nogui
The minecraftlog file does update at each boot, so the script is run.
When I run the command "sudo sh startspigot.sh", everything works perfectly. The screen is started and the minecraftlog file is updated. I can find the screen again with "sudo screen -ls"
At boot, however, both "sudo screen -ls" and "screen -ls" return no sockets.
What can be causing this? The only two users are "pi" and root.
Thanks in advance!
Starting a script in a new detached screen as current user (rc.local = root):
screen -dmS <session name> <command>, example:
screen -dmS screenName bash /home/user/run.sh
Starting a script from rc.local as user:
runuser -l user -c 'screen -dmS screenName bash /home/user/run.sh'
Running screen in detached mode (when you do not have active terminal, like in rc.local or crontab):
screen -dm -S <session name> <command>
-d -m Start screen in "detached" mode
-S When creating a new session, this option can be used to specify a meaningful name for the session.

Server Startup Script Problems

I am trying to set up a Minecraft server. However, the basic startup scripts provided do not fit my needs. I want a script that will:
Start a new screen running the jarfile and (pretty much) only the jarfile (so i can ^C it if needed without killing other things like screen or my gzip commands)
Gzip any logs that weren't gzipped automatically by the jarfile (for if/when i ^C'ed the server, or if it crashed)
Run a command with sudo to set the process in the first argument to a high priority (/usr/bin/oom-priority)
Run a http-server on the resource-pack directory in a different screen and send ^C to it when the server closes
I have these three commands. I run startserver to start the server.
startserver:
#!/bin/bash
set -m
cd /home/minecraftuser/server/
echo
screen -dm -S http-server http-server ./resource-pack
screen -dm -S my-mc-server startserver_command
(sleep 1; startserver_after) &
screen -S my-mc-server
startserver_command:
#!/bin/bash
set -m
cd /home/minecraftuser/server/
echo
java -Xmx768M -Xms768M -jar ./craftbukkit.jar $# &
env MC_PID=$! > /dev/null
(sleep 0.5; sudo /usr/bin/oom-priority $MC_PID) &
fg 1
echo
read -n 1 -p 'Press any key to continue...'
and startserver_after:
#!/bin/bash
cd /home/minecraftuser/server/
wait $MC_PID
find /home/minecraftuser/server/logs -type f -name "*.log" -print | while read file; do gzip $file &
done
screen -S http-server -p 0 -X stuff \^c\\r
Edit: When I run startserver, I get a command prompt then a bunch of gzip errors of files already existing (I am expecting these errors, but when I run startserver I'm supposed to get the java program). Somehow I am in a screen because when I do ^A d, I am brought to a new prompt.
Once I am out of the screen, screen -ls returns two instances of my-mc-server. One is a blank command prompt, the other is the server running successfully.
Edit 2: I changed startserver_command to remove the asterisk from env MC_PID=$! & (not needed there) and added it to (sleep 1; startserver_after) (makes it faster), redirected env line to /dev/null (removes entire environment listing at beginning of output). Still didn't fix the entire problem.
Instead of starting each screen session from the scripts, you can just use a custom .screenrc to specify some startup windows (and to run commands/scripts):
#$HOME/mc-server.screenrc
screen -t http-server 0 'startserver'
screen -t my-mc-server 1 'startserver_command'
screen -t gzip-logs 2 'startserver_after'
Then simply start screen (specifying the config file to use, if it's not the default ~/.screenrc)
screen -dm -c mc-server.screenrc

Using screen in bash script

I'm running a game server on a remote server where I use a detached screen instance to leave it running.
I'm now creating a script that can be used to shut down the server, back up all the vital files and start it up again, however I'm having a few difficulties with dealing with the screen.
I assumed that I could just switch into the detached screen in the script (after the server had already been shut down) by calling screen -r in the script.
But that doesn't seem to work because if I run the script from outside screen it just launches the server in that session.
screen -r
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
screen -d
This is what I thought would do the trick but it doesn't. Maybe somebody can help me out here. I'm not a bash expert. In fact this is propably my first bash script that doesn't include "Hello World". Thanks.
Your script, as in your example, will get executed by your sell, not the one in the screen. You need to tell the running screen to read a file and execute it - that's what the -X option is for.
Try
tempfile=$(mktemp)
cat > $tempfile <<EOF
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
EOF
screen -X readbuf $tempfile
screen -X paste .
rm -f $tempfile
You can leave screen running in a 2nd terminal session to see what happens.

Resources