Running .sh script at boot

Ask questions about dedicated servers here and we and other users will do our best to answer them. Please also refer to the self-help section for tutorials and answers to the most commonly asked questions.
Post Reply
EliTheGamerFTW
New to forums
New to forums
Posts: 5
https://www.youtube.com/channel/UC40BgXanDqOYoVCYFDSTfHA
Joined: Tue Jun 05, 2018 8:42 am

Running .sh script at boot

Post by EliTheGamerFTW »

Hi there
So, I've been having some trouble getting my Ubuntu 18.04 x64 VDS to run a server startup script at boot. Crontab for some reason doesn't work (and throws no errors either), using /etc/init.d and systemd also refuse to work and throw no errors. The script works exactly the way it should, which is where it's confusing me. Anyone know another good way to run commands at boot? (Note, this is in case the machine crashes, so it has to be at boot and not at login).
Thanks!
User avatar
hiimcody1
Staff
Staff
Posts: 1595
Joined: Wed Dec 28, 2011 4:59 pm

Re: Running .sh script at boot

Post by hiimcody1 »

Crontab is the tried and true way for doing this. I personally use it on my Ubuntu VDS with zero issues.

Can you include the source code for the script you are running? (redact passwords or sensitive info if needed)
EliTheGamerFTW
New to forums
New to forums
Posts: 5
Joined: Tue Jun 05, 2018 8:42 am

Re: Running .sh script at boot

Post by EliTheGamerFTW »

No problem.

Crontab:

Code: Select all

@reboot /server/autorun.sh
autorun.sh

Code: Select all

if ! screen -list | grep -q "hl2rp"; then
        echo "Screen session not found. Starting server."
 bash /server/server.sh
else
        echo "Screen session found. Waiting for next check."
fi
/server/server.sh

Code: Select all

#!/bin/sh
echo "Starting HL2RP server."
echo "Use \"screen -x hl2rp\" to load the server console."
echo "While in the screen, press Ctrl-A then d to detach."
sleep 1
screen -A -m -d -S hl2rp (server run code)
User avatar
hiimcody1
Staff
Staff
Posts: 1595
Joined: Wed Dec 28, 2011 4:59 pm

Re: Running .sh script at boot

Post by hiimcody1 »

For autorun.sh, try throwing a command interpreter at the beginning of it:

Code: Select all

#!/bin/sh
User avatar
Edge100x
Founder
Founder
Posts: 12947
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Running .sh script at boot

Post by Edge100x »

You should also make sure that your /server/autorun.sh script has chmod +x, and you might consider having the crontab entry change to the folder of the script before running it.

If you run this script by hand from /root, does it do what you'd like it to do?
EliTheGamerFTW
New to forums
New to forums
Posts: 5
Joined: Tue Jun 05, 2018 8:42 am

Re: Running .sh script at boot

Post by EliTheGamerFTW »

Yeah, running it by hand works exactly how I want. I'll post results from using the command interpreter.
EliTheGamerFTW
New to forums
New to forums
Posts: 5
Joined: Tue Jun 05, 2018 8:42 am

Re: Running .sh script at boot

Post by EliTheGamerFTW »

So, adding the interpreter makes it run, however the screen session responds as dead each time.
User avatar
Edge100x
Founder
Founder
Posts: 12947
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Running .sh script at boot

Post by Edge100x »

When you say is "responds as dead", what do you mean?

Also, note that your comment says that you are using "screen -x" to access the screen. The standard way is to use "screen -r".
EliTheGamerFTW
New to forums
New to forums
Posts: 5
Joined: Tue Jun 05, 2018 8:42 am

Re: Running .sh script at boot

Post by EliTheGamerFTW »

By responds as dead, I mean the screen session is marked as dead under screen -ls, and I have to screen -wipe and then start the server again.
User avatar
Edge100x
Founder
Founder
Posts: 12947
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Running .sh script at boot

Post by Edge100x »

That usually happens after server crashes, when screen doesn't have the chance to clean up its old sessions. Do you have anything running that would kill it?

You also might try adding a delay to your script, before any commands are executed (say, 5 seconds). It is possible that cron starts early in the boot process, before another process is run that destroys old screen sessions.
Post Reply