Run a shell script in Terminal through a keyboard/desktop shortcut - linux

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.

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.

bash script to auto run on boot, make screen, execute a command and detach

I am using Centos 7 and on boot I would like to:
Make a screen
Execute a command: osrm-routed --algorithm=MLD
~/osrm-backend/profiles/australia-latest.osrm
Detatch from screen (possibly not needed, just as long as I can
access it myself after its running in future)
Here is something I have thought about, although not correct and wont work
filename: mapstart.sh
Contents of file:
#!/bin/bash
/usr/bin/screen -dmS mapapi osrm-routed --algorithm=MLD ~/osrm-backend/profiles/australia-latest.osrm
With your help with the script. I am not sure the best way to run that on boot with centos 7.
Appreciate your help and input.
For those who would like to know. The issue was with OSRM and centos. I was able to get it running using the full paths of everything and the following in crontab -e
To get the full path of osrm-backend i ran the command of:
which osrm-routed
It returned the result of:
/usr/local/bin/osrm-routed
That then enabled me to add the full path of the command I was trying to run from crontab -e which is required. From there it worked running the below in crontab -e
#reboot /usr/bin/screen -dm -S pistartup /usr/local/bin/osrm-routed --algorithm=MLD ~/osrm-backend/profiles/australia-latest.osrm
break down of all the above:
runs command at reboot only:
#reboot
full path to screen command:
/usr/bin/screen
create screen with name of pistartup and detach:
-dm -S pistartup
my particular command i wanted to run inside the screen:
/usr/local/bin/osrm-routed --algorithm=MLD ~/osrm-backend/profiles/australia-latest.osrm
Now when ever the machine is rebooted. it has created a screen and run my command. To resume the screen manually If i ever wanted to, i could issue the command of:
screen -r pistartup

Run Linux command in background and keep runing after closing SSH [duplicate]

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

"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

Having problems running python script even when putty is closed

So basically, I have a bot I'm running, and I would like for it to keep running even when I exit putty.
I've tried using nohup python bot.py & but it still ends the python bot when I close the putty program. I've also tried using a run.sh file with /usr/bin/nohup bot.py & inside it. but it won't work :( is there something else I'm missing?
I have also made sure the run.sh is a executable as some other forums have suggested, and I still get can't open run
I'm kinda new to the linux terminal.
if you guys could help me out that would be awsome :)
You need to detach the terminal so that when you exit, it is still running. You can use screen or tmux or some other multiplexer.
Here is how to do with screen:
screen -S mybot -m -d /usr/bin/python /path/to/bot.py
-S give the session a name (this is useful if you want to attach later. screen -D -R mybot)
-m always create a new session
-d detach (launch the program, but then detach the terminal returning you to the prompt)

Resources