Jump to content

A Bash Script Run Over SSH Will Not Keep Running

Go to solution Solved by mr cheese,
Just now, TheCoder2019 said:

Is there a way I can keep my server open without having command prompt open all the time?

This is how SSH works. You open a separate terminal instance in which your commands are run, and when you close out ssh, the terminal is closed as is everything spawned in it.

The solution I've found to be easiest is using screen, which you can install through any default package manager for the most part. Once installed, you just run

screen [command you want to run]

so, in your case, I would run

screen ./script.sh

or whatever your script's name/extension is, then it'll clear the screen and spawn a new, detachable terminal instance you can disconnect from and reconnect to at will. If you disconnect from the instance (you press ctrl+a, then press d) or close out of the ssh shell, you can reconnect by running

screen -r

and you'll be good to go. As always, be sure to read up on it so you understand it well enough, but it's what I've been using to manage my Minecraft server instances over ssh for years now with very little extra hassle.

So I have a MC server. ok.

When I ssh into it, no problems.

When I run the bash script, it runs.

When I close command prompt, it stops.

Is there a way I can keep my server open without having command prompt open all the time?

My script:

Spoiler

umount -fR /dev/sdc1/
mount /dev/sdc1 /home/pi/MCServer/
systemctl restart smbd
echo -e "\033[1;32m Starting server..."
cd ./MCServer/nukkit
cd ./MCServer/Nukkit
sudo java -jar ./target/nukkit-1.0-SNAPSHOT.jar

 

 

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

Just now, TheCoder2019 said:

Is there a way I can keep my server open without having command prompt open all the time?

This is how SSH works. You open a separate terminal instance in which your commands are run, and when you close out ssh, the terminal is closed as is everything spawned in it.

The solution I've found to be easiest is using screen, which you can install through any default package manager for the most part. Once installed, you just run

screen [command you want to run]

so, in your case, I would run

screen ./script.sh

or whatever your script's name/extension is, then it'll clear the screen and spawn a new, detachable terminal instance you can disconnect from and reconnect to at will. If you disconnect from the instance (you press ctrl+a, then press d) or close out of the ssh shell, you can reconnect by running

screen -r

and you'll be good to go. As always, be sure to read up on it so you understand it well enough, but it's what I've been using to manage my Minecraft server instances over ssh for years now with very little extra hassle.

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

2 minutes ago, DaJakerBoss said:

This is how SSH works. You open a separate terminal instance in which your commands are run, and when you close out ssh, the terminal is closed as is everything spawned in it.

The solution I've found to be easiest is using screen, which you can install through any default package manager for the most part. Once installed, you just run


screen [command you want to run]

so, in your case, I would run


screen ./script.sh

or whatever your script's name/extension is, then it'll clear the screen and spawn a new, detachable terminal instance you can disconnect from and reconnect to at will. If you disconnect from the instance (you press ctrl+a, then press d) or close out of the ssh shell, you can reconnect by running


screen -r

and you'll be good to go. As always, be sure to read up on it so you understand it well enough, but it's what I've been using to manage my Minecraft server instances over ssh for years now with very little extra hassle.

So for me I'd run

sudo apt-get install screen -y

Then run

sudo screen ~/ReNAS

Right? Am I reading that right?

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

6 minutes ago, TheCoder2019 said:

So for me I'd run



sudo apt-get install screen -y

Then run



sudo screen ~/ReNAS

Right? Am I reading that right?

is ReNAS the name of this script? why are you running screen as a superuser

 

19 minutes ago, TheCoder2019 said:

My script:

  Hide contents




umount -fR /dev/sdc1/
mount /dev/sdc1 /home/pi/MCServer/
systemctl restart smbd
echo -e "\033[1;32m Starting server..."
cd ./MCServer/nukkit
cd ./MCServer/Nukkit
sudo java -jar ./target/nukkit-1.0-SNAPSHOT.jar

 

 

 

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

2 minutes ago, DaJakerBoss said:

is ReNAS the name of this script? why are you running screen as a superuser

 

 

ReNAS is the name of the script. It "ReNASes" my Pi. I need it run as superuser because the jarfile needs root for hosting on a port (19132)

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

Just now, TheCoder2019 said:

ReNAS is the name of the script. It "ReNASes" my Pi. I need it run as super user because the jarfile needs root for hosting on a port (19132)

in that case yes, that's what you would run

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

8 minutes ago, DaJakerBoss said:

in that case yes, that's what you would run

I'll try it. If this returns a Boolean True and a byte array of 0x0 errors I will mark first reply as Boolean True Answer

 

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

15 minutes ago, TheCoder2019 said:



sudo screen ~/ReNAS

 

 

Also if you run screen with -Rd SessionName then it will allow you to easily reconnect to the active session later.

ASUS B650E-F GAMING WIFI + R7 7800X3D + 2x Corsair Vengeance 32GB DDR5-6000 CL30-36-36-76  + ASUS RTX 4090 TUF Gaming OC

Router:  Intel N100 (pfSense) Backup: GL.iNet GL-X3000/ Spitz AX Switches: Netgear MS510TXUP, MS510TXPP, GS110EMX
WiFi6: Zyxel NWA210AX (1.7Gbit peak at 160Mhz) WiFi5: Ubiquiti NanoHD OpenWRT (~500Mbit at 80Mhz)
ISPs: Zen Full Fibre 900 (~930Mbit down, 115Mbit up) + Three 5G (~1200Mbit down, 115Mbit up, variable)
Upgrading Laptop/Desktop CNVIo WiFi 5 cards to PCIe WiFi6e/7

Link to post
Share on other sites

@DaJakerBoss is there a raspberry pi version?

E460CDC6-7DB3-4D85-8707-40548C4EFEB1.png

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

2 minutes ago, TheCoder2019 said:

@DaJakerBoss is there a raspberry pi version?

E460CDC6-7DB3-4D85-8707-40548C4EFEB1.png

well, i mean it's telling you what to do, run apt-get update

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

Just now, DaJakerBoss said:

well, i mean it's telling you what to do, run apt-get update

wait a minute are you sending ssh commands over... ios shortcuts

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

9 minutes ago, Alex Atkin UK said:

 

Also if you run screen with -Rd SessionName then it will allow you to easily reconnect to the active session later.

if you only have one screen instance you should be okay just running screen -r because it reattaches to the last active session

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

2 minutes ago, DaJakerBoss said:

wait a minute are you sending ssh commands over... ios shortcuts

yes! Also here, I ran it... too bad I can't see console live

E1FCFD3A-CFD7-407A-A161-80CA0E8E15B1.png

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

Just now, TheCoder2019 said:

yes!

...is that safe because i do not endorse this

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

3 minutes ago, DaJakerBoss said:

if you only have one screen instance you should be okay just running screen -r because it reattaches to the last active session

True, but its something I wish I had known when I first started using screen. 😉

ASUS B650E-F GAMING WIFI + R7 7800X3D + 2x Corsair Vengeance 32GB DDR5-6000 CL30-36-36-76  + ASUS RTX 4090 TUF Gaming OC

Router:  Intel N100 (pfSense) Backup: GL.iNet GL-X3000/ Spitz AX Switches: Netgear MS510TXUP, MS510TXPP, GS110EMX
WiFi6: Zyxel NWA210AX (1.7Gbit peak at 160Mhz) WiFi5: Ubiquiti NanoHD OpenWRT (~500Mbit at 80Mhz)
ISPs: Zen Full Fibre 900 (~930Mbit down, 115Mbit up) + Three 5G (~1200Mbit down, 115Mbit up, variable)
Upgrading Laptop/Desktop CNVIo WiFi 5 cards to PCIe WiFi6e/7

Link to post
Share on other sites

1 minute ago, DaJakerBoss said:

...is that safe because i do not endorse this

I assume it is, why? I'm not signed into iCloud or anything

Also see edit above 

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

1 minute ago, TheCoder2019 said:

I assume it is, why? I'm not signed into iCloud or anything

Also see edit above 

you could just install termius its a free ssh app and it's an actual terminal...

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

Just now, DaJakerBoss said:

you could just install termius its a free ssh app and it's an actual terminal...

Well, umm... school mdm and I can't turn my PC on at 2:17 AM, kinda don't wanna get caught

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

Just now, TheCoder2019 said:

Well, umm... school mdm and I can't turn my PC on at 2:17 AM, kinda don't wanna get caught

hey, if youre comfortable with it, go for it. anyways now that you've updated your apt lists just try to install screen again and go from there

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

2 minutes ago, DaJakerBoss said:

hey, if youre comfortable with it, go for it. anyways now that you've updated your apt lists just try to install screen again and go from there

Oh god, I may have to sudo reboot...

F617AEF3-BA90-40AF-9F12-1834C4274C5A.png

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

1 minute ago, TheCoder2019 said:

Oh god, I may have to sudo reboot...

F617AEF3-BA90-40AF-9F12-1834C4274C5A.png

this is why you should use an actual shell when doing these kind of things. it's pretty likely it didn't get to finish what it was doing and got stuck. in the future... don't use shortcuts for ssh...

My profile picure is real. That's what I look like in real life. I'm actually a blue and white African Wild Dog.

Ryzen 9 5900X - MSI Ventus 2x OC 3060 Ti - 2x8GB Corsair Vengeance LPX 3200MHz CL16 - ASRock B550 Phantom Gaming ITX/ax

EVGA CLC 280 + 2x140mm NF-A14 - Samsung 850 EVO 500GB + WD Black SN750 1TB - Windows 11/10 - EVGA Supernova G3 1000W

Link to post
Share on other sites

7 minutes ago, DaJakerBoss said:

this is why you should use an actual shell when doing these kind of things. it's pretty likely it didn't get to finish what it was doing and got stuck. in the future... don't use shortcuts for ssh...

Do I have a bricked OS now? Please say no...

I can probably use an old laptop

 

EDIT: well do I reboot now?

 

image.jpg

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

48 minutes ago, DaJakerBoss said:

this is why you should use an actual shell when doing these kind of things. it's pretty likely it didn't get to finish what it was doing and got stuck. in the future... don't use shortcuts for ssh...

Ayyy! I'll try it and let you know!

image.jpg
EDIT: Well every time I run my script Screen decides to terminate itself

EDIT 2: I ended up using TMUX instead... I found it easier to use...

specs

Spoiler

ASRock B650I Lightning WiFi

AMD Ryzen 7 7700X

KLEVV CRAS V RGB (2x16GB) 6000MT/s out of the 7200MT/s I could be running

ASUS RTX 3060 OC (12GB)

Thermalright Frozen Edge 240 RGB

Lian Li A4-H2O

 

linux packages

Spoiler

tmux

btop

git

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×