Running .sh script at boot
-
- New to forums
- Posts: 5
- https://www.youtube.com/channel/UC40BgXanDqOYoVCYFDSTfHA
- Joined: Tue Jun 05, 2018 8:42 am
Running .sh script at boot
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!
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!
Re: Running .sh script at boot
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)
Can you include the source code for the script you are running? (redact passwords or sensitive info if needed)
-
- New to forums
- Posts: 5
- Joined: Tue Jun 05, 2018 8:42 am
Re: Running .sh script at boot
No problem.
Crontab:
autorun.sh
/server/server.sh
Crontab:
Code: Select all
@reboot /server/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
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)
Re: Running .sh script at boot
For autorun.sh, try throwing a command interpreter at the beginning of it:
Code: Select all
#!/bin/sh
Re: Running .sh script at boot
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?
If you run this script by hand from /root, does it do what you'd like it to do?
-
- New to forums
- Posts: 5
- Joined: Tue Jun 05, 2018 8:42 am
Re: Running .sh script at boot
Yeah, running it by hand works exactly how I want. I'll post results from using the command interpreter.
-
- New to forums
- Posts: 5
- Joined: Tue Jun 05, 2018 8:42 am
Re: Running .sh script at boot
So, adding the interpreter makes it run, however the screen session responds as dead each time.
Re: Running .sh script at boot
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".
Also, note that your comment says that you are using "screen -x" to access the screen. The standard way is to use "screen -r".
-
- New to forums
- Posts: 5
- Joined: Tue Jun 05, 2018 8:42 am
Re: Running .sh script at boot
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.
Re: Running .sh script at boot
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.
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.