[Basic] How to: Start a Program or Script on Linux Automatically on Boot with systemd


TABLE OF CONTENTS


If you have a Linux server and have a need to configure a service or program to automatically start on boot, this guide will demonstrate how to do so. This guide will be helpful to those that run critical services on their Linux machines. You will learn 

how to ensure that those services always auto-start, even after an unplanned reboot.


How to start a program on Linux automatically on boot


We will be utilizing systemd for this task. First, we will create a sample script. Next, we will create a system unit that references our sample script. Finally, we will tell systemd, to automatically run our script (service unit) on system startup.


Start by logging onto your VPS via SSH. You may follow this guide to help you SSH into your SkySilk VPS


Create the sample script or program that we want to automatically start on boot


Create the following script in /usr/sbin/testscript.sh using your favorite text editor (like nano):


#!/bin/bash
d=$(date +%Y-%m-%d-%s)
echo "$d" >> ~/"$d".log
exit 0


Make sure that the script is executable:


chmod +x /usr/sbin/testscript.sh

Create a system unit (also known as a service)


We will now create a system unit that references the above test script. This system unit is how systemd will know to run the test script at system boot.


Create the following file in /etc/systemd/system/test.service.  Make sure you reference the script that you created above and that you save the system unit with a ".service" extension.


[Unit]
Description=A description for your custom service goes here
After=network.target

[Service]
Type=simple
ExecStart=/bin/bash /usr/sbin/testscript.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

Configure your service to automatically start on boot

Reload the systemctl daemon:


 systemctl daemon-reload


Tell systemd to enable your custom system unit:


 systemctl enable test.service


Confirm that your test service was created successfully:


systemctl --all | grep test.service


Start your service:


systemctl start test.service 

Test your service with a reboot!


Go ahead and do a test reboot of your server. The service you configured should auto-start as the server boots up. At this point your all done with the configuration!  




Join our Private Discord Chat to chat with, as well as find community assistance from other Verified SkySilk Users:
https://invite.gg/SkySilk 


CLICK TO DEPLOY AN UBUNTU LINUX VPS


Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.