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

vi /etc/systemd/system/sok-startup.service

Add

[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

#!/bin/bash

touch /root/OK

exit 0

You can replace “touch /root/OK” with whatever command you need to execute.

Make it executable with

chmod 755 /etc/rc.local

Reload systemd

systemctl daemon-reload

Enable the service

systemctl enable sok-startup.service

Now reboot the server, you will see /root/OK get created.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *