Run a script on boot using systemd on Ubuntu 18.04
Previous versions of Ubuntu had /etc/rc.local. that get executed after system boot.
On Ubuntu 18.04, you can use systemd to start a bash script on system boot.
Create file
1 |
vi /etc/systemd/system/sok-startup.service |
Add
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=Start up script ConditionPathExists=/etc/rc.local [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 StandardOutput=tty RemainAfterExit=yes SysVStartPriority=99 [Install] WantedBy=multi-user.target |
Create file /etc/rc.local with
1 2 3 4 5 |
#!/bin/bash touch /root/OK exit 0 |
You can replace “touch /root/OK” with whatever command you need to execute.
Make it executable with
1 |
chmod 755 /etc/rc.local |
Reload systemd
1 |
systemctl daemon-reload |
Enable the service
1 |
systemctl enable sok-startup.service |
Now reboot the server, you will see /root/OK get created.