🏒

Executing commands on bootup

Created
2020/12/26 07:48
We sometimes want Ubuntu machine to conduct some tasks on startup. In my case, I wanted my Ubuntu machine to create a reverse SSH tunnel on startup so that I don't have to create one manually, or that I could connect to machine after rebooting it remotely. The command was like autossh ~~
As a result, it was /etc/rc.local. Ubuntu executes /etc/rc.local script on startup, if exists. On Ubuntu 18.04 there is no such pre-existing script file, different from its predecessors, but I could manually create one instead. Note that we need to end the script file with the code like exit 0, like below:
# /etc/rc.local autossh ~~ exit 0
Shell
복사
And you may have to make the file executable by the command sudo chmod +x /etc/rc.local.
But still I could not connect to the machine after rebooting. The problem was, I wanted to connect to the machine as a user, not root, when the command was executed by root. Fortunately, the solution was easy. I just could execute the command as a user, using the code like su USER -c 'COMMAND'. So the final result was like below:
# /etc/rc.local su USER -c 'autossh ~~' exit 0
Shell
복사
Now, I don't have to worry about opening the tunnel every time after rebooting machine, and can reboot the machine remotely, just in case. Cool.
But for Ubuntu 20.04, it seems that /etc/rc.local does not basically working. For the case you should check the link.
E.O.D.