TABLE OF CONTENTS
- How to start a program on Linux automatically on boot
- Test your service with a reboot!
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:
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!