I have a java program that starts a TCP/IP game server.
I have an angular application that speaks with an express node.js server to start/stop the game server.
When the button start is clicked the following operation is performed in the nodejs server:
shell.exec('/web/starter.sh');
And this is my script:
:set fileformat=unix
#!/bin/sh
screen -RD server -X quit;
screen -dmS server;
cd ~/cadesim-monomer;
java -jar CadeServerEngine.jar;
It works, it starts the server. But whenever I click start again, it doesn't restart the server as it should. It should kill the screen and restart?
What did I do wrong?
The issue is that the server commands aren't running inside the detached screen session. They are running after both of the screen commands.
To run a command in detached screen session for this example would need to use:
screen -dmS server sh -c "cd ~/cadesim-monomer; java -jar CadeServerEngine.jar"
Or you can put the server and its setup in its own shell file and that file would have a similar appearance to a daemontools run script. And then you would have a command like:
screen -dmS server ./server.sh
Also the shebang line should be the first line in the file. The script just happens to work because the shell is the default interpreter. It produces the error line 1: :set: command not found. The VIM mode setting should occur in a comment on the second line.
Related
I'm using Linux (Mint 20.3) to run a simple Minecraft server and I want to be able to start the server with a keyboard or desktop shortcut. I also want to be able to interact with the server in a terminal after it starts. I'm currently using the server software supplied by Mojang. I wrote a little program to get things started:
#!/bin/bash
cd /home/trevor/Minecraft_Server
LD_LIBRARY_PATH=. ./bedrock_server
exec $SHELL
I can get the server to run but I have no clue how to get it to open a terminal window so I can interact with the server. I'm relatively new to Linux so any input would be greatly appreciated.
you can use screen to detach and attach to run commands into the minecraft terminal.
to install screen:
apt-get install -y screen
To launch, update your script with something like:
screen -S mcs ./bedrock_server
to reattach, run the following in a terminal:
screen -r mcs
Use screen in your script to reattach to bedrock process.
Install screen:
apt-get install screen
Define your script as:
#!/bin/bash
export LD_LIBRARY_PATH=.
cd /home/trevor/Minecraft_Server
screen -d -m -S bedrock ./bedrock_server
After invoke your script, screen creates a socket that can be used to reattach to your script terminal. You can show screen sockets available with:
screen -ls
Parameter -S defined 'bedrock' as the socket name. So you can open another terminal as you like and reattach to the bedrock process with:
screen -r bedrock
If you detach the screen with CTRL+C the screen will be closed and so the minecraft bedrock server. To deattach without close the process you must use CTRL+A and CTRL+D.
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.
This question already has answers here:
How to make a program continue to run after log out from ssh? [duplicate]
(6 answers)
Closed 3 years ago.
I need to run a Perl script for several days processing something. On a linux Centos server, from the SSH terminal I run this command:
nohup perl script.cgi 2>&1 &
This runs the script in the background and writes the output to nohup.out.
The problem when I close the SSH terminal or even my internet connection disconnects the script terminates.
I need to keep this command running in the background on the server after I close
the SSH terminal.
You can use Terminal multiplexer tools like screen, byobu or tmux.
I personally use screen. so install it on remote server via sudo apt-get install screen.
ssh into server
open screen session by screen -S sessionname
Now run your command (background/foreground both works)
now detach to your session by command ctrl+a then press d.
Now shut your pc and enjoy
now come back ssh into server then use command screen -x sessionname to reconnect the detached session.
Hurray! script is still running.
you can either use screen or run the command using supervisor in linux systems.
you can install screen using sudo apt install screen
then use following command to run it.
screen -S test_command
nohup perl script.cgi 2>&1 &
Then press ctrl+a and ctrl+d to leave that session running for whatever amount of time required until your server reboots.
If you want to stop the command use screen -x test_command, then ctrl+c and use ctrl+a and ctrl+d to close screen or ctrl+a and ctrl+d to leave the screen session as it is.
The only way I was able to run the command and exit the shell and keep the command running is using the "at" tool to schedule the job like this using the full script path:
echo "perl /home/username/www/script.cgi" | at now + 1 minute
I want to run "screen" on a debian linode server, starting up over a ssh terminal window. I'd like a shell script to start and detach a screen, so that a process can continue when I log off. I'd also like the logging file screenlog.0 to be produced, so that there's a record if the process crashes.
But there's a problem in getting the log file to write. Locally, on a mac terminal window,
% screen -dm -L sh -c 'echo hello'
works fine, "hello" gets written to screenlog.0. But the same command issued in a ssh window to the server executes, but nothing gets written.
However, if in that window I go into screen,
% screen -L
and then do some stuff, the activity is written to screenlog.0 (on the server).
What am I missing?
It turns out that the screen() command can have problems. The above command sends no output to screenlog.0 under 'Debian GNU/Linux 9 (stretch)' , while 'Ubuntu 14.04.1 LTS' writes the odd message, "error: could not start server! try running as root!", to screenlog.0, even when running as root. 'Linux Mint 18.1' and MacOSX run correctly.
I was advised to use the venerable unix command "nohup" to solve my problem of detaching a process and logging its output, even when you close the ssh connection. Ordinarily, when you close a terminal window, the signal SIGHUP is sent to any processes that were started there. But
% nohup myprog > logfile.txt &
works perfectly. Old way, good way.
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