rc.local not running on server boot
I have an issue with my rc.local, which isn't running automatically the command line contained inside, on server boot. However, running manually rc.local works fine and starts the script.
Operating System: Ubuntu 20.04.2 LTS
Here is my rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sh '/home/[user]/azerothcore-wotlk/reboot.sh'
exit 0
And my reboot.sh
#!/bin/sh
/usr/bin/screen -AmdS world /home/[user]/azerothcore-wotlk/acore.sh run-worldserver
/usr/bin/screen -AmdS auth /home/[user]/azerothcore-wotlk/acore.sh run-authserver
Status of the rc.local service:
$ systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/rc-local.service.d
└─debian.conf
Active: active (exited) since Wed 2021-05-19 22:01:41 IST; 2 days ago
Docs: man:systemd-rc-local-generator(8)
Process: 500 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
Warning: some journal files were not opened due to insufficient permissions.
rc.local and reboot.sh both have permissions:
$ ls -l /etc/rc.local
-rwxr-xr-x 1 root root 718 May 16 01:13 /etc/rc.local
$ ls -l reboot.sh
-rwxrwxr-x 1 [user] [user] 179 May 16 01:16 reboot.sh
By the way rc.local used to work awhile ago, but suddenly stopped for an unknown reason. I'm not an experienced Linux user, so I've read a lot of posts about this problem, and the most often recommendations was to: check if the rc.local has a shebang line (check!); Set proper permissions to rc.local and the script (check!); check the status of the service (check!).
After hitting a rock with rc.local, I decided to give a try to some other methods.
I tried with crontab:
crontab -e
@reboot sh /home/[user]/azerothcore-wotlk/reboot.sh
Not starting on server boot. Starting normally if run manually.
Also tried init.d
sudo su
nano /etc/init.d/reboot
contents:
#!/bin/sh
/home/[user]/azerothcore-wotlk/reboot.sh
Then:
chmod ugo+x /etc/init.d/reboot
update-rc.d reboot defaults
Same result - not starting on boot.
Then tried to create a service:
sudo nano /usr/bin/acore.sh
contents:
#!/bin/sh
screen -AmdS world /home/[user]/azerothcore-wotlk/acore.sh run-worldserver
screen -AmdS auth /home/[user]/azerothcore-wotlk/acore.sh run-authserver
sudo chmod +x /usr/bin/acore.sh
sudo nano /etc/systemd/system/acore.service
contents:
[Unit]
Description=My daemon
[Service]
ExecStart=/usr/bin/acore.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
$ systemctl start acore.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to start 'acore.service'.
Authenticating as: ,,, ([user])
Password:
==== AUTHENTICATION COMPLETE ===
systemctl enable mydaemon.service
systemctl enable acore.service
Not working either.
At this point I have absolutely no idea what to do. As I mentioned above, I'm not an experienced Linux user, so any help will be appreciated!
Thank you!
Edit: As @terdon suggested, I added the lines
/usr/bin/touch /tmp/rc.has.run
to the beginning of the rc file)
and
/usr/bin/touch /tmp/reboot.has.run
to the beginning of the reboot script
Both files have been created in the /tmp
folder after rebooting the server.
Edit2: Thanks to @terdon the problem is solved by adding 'sleep 60' in the beginning of the rc.local.
It looks like the problem isn't that the script isn't running, but that it is running too soon, before the system has finished setting everything up, and therefore fails because your machine isn't ready to run it yet. So a simple solution is to add a delay to the script so that it waits a bit before launching.
Edit your reboot.sh
script, and add a sleep
call:
#!/bin/sh
## wait for 60 seconds
sleep 60
/usr/bin/screen -AmdS world /home/[user]/azerothcore-wotlk/acore.sh run-worldserver
/usr/bin/screen -AmdS auth /home/[user]/azerothcore-wotlk/acore.sh run-authserver
You can experiment with the sleep
value to find something that works and doesn't feel slow.