Greetings, fellow program enthusiasts! Today, let’s explore systemd, the trusty conductor orchestrating your server’s startup symphony. This guide shows the process of automating program launches during boot on Ubuntu, ensuring a seamless performance every time your server takes the spotlight. Follow me as we learn how to auto-start your programs using systemd.
Let’s jump straight into action, beginning with the creation of a system service file—a vital script that dictates how your programs take the stage during boot.
In your terminal, run the following to create your .service file and open the editor:
sudo nano /etc/systemd/system/ProgramNameHere.service
Within this file, you’ll craft the code to run your program:
[Unit]
Description=Program Name Description
After=network.target
[Service]
ExecStart=/bin/bash -c 'cd /path/to/program_folder && program.sh'
WorkingDirectory=/path/to/program_folder
Restart=always
User=username
Group=groupname
[Install]
WantedBy=multi-user.target
Using the above information as a template, input your program parameters and save your file.
Let’s break down the sections in this service file:
[Unit] Section:
- Description: A brief, human-readable description of the service.
- After: Specifies the service’s startup order, ensuring it kicks in after the network is up.
[Service] Section:
- ExecStart: Specifies the full path to your program.
- WorkingDirectory: Sets the working directory to your program’s location.
- User and Group: Define the user and group under which the program should run.
[Install] Section:
- WantedBy: Specifies the target (e.g., multi-user.target) crucial for enabling the service during startup.
Here’s a real-world example using a Python-based mining program, Duino Coin.
If you want to try the example as well, if you are following along step by step, get the program files for Duino Coin here: https://duinocoin.com/getting-started.html
Simply install the program to ensure it is configured and runs, then follow the example below:
sudo nano /etc/systemd/system/duino-coin.service
Here is the code we will use for the program auto-start file:
[Unit]
Description=Duino Coin Program
After=network.target
[Service]
ExecStart=/bin/bash -c 'cd /home/ubuntu/duino-coin && python3 PC_Miner.py'
WorkingDirectory=/home/ubuntu/duino-coin
Restart=always
User=ubuntu
Group=ubuntu
[Install]
WantedBy=multi-user.target
Once the file is saved, reload the daemon and enable the new service (using Duino Coin as an example):
sudo systemctl daemon-reload
sudo systemctl enable duino-coin
sudo systemctl restart duino-coin
To check the status:
sudo systemctl status duino-coin
Remember to adjust paths and configurations as needed for your specific setup.
For quick reference on working directory, username, and primary group ID:
# Working directory and program directory
pwd
# Username
whoami
# Primary group
id -gn
Troubleshooting tip: If the service doesn’t start as expected, checking the output of sudo journalctl -xe
can provide additional information.
Python Virtual Environments:
For those working with Python programs in virtual environments, adapting the [Service] section is straightforward. Here’s an example:
[Service]
ExecStart=/bin/bash -c 'cd /home/ubuntu/duino-coin && source myvenv/bin/activate && python3 PC_Miner.py'
WorkingDirectory=/home/ubuntu/duino-coin
Restart=always
User=ubuntu
Group=ubuntu
Now, armed with this guide, let your programs gracefully take the stage with each server boot! systemd can do so much more than auto-start; more information can be found here on systemd’s website: https://systemd.io/
There you go! This is systemd! These are simple skills to auto-start your programs using systemd. Whether you’re a seasoned server maestro or a curious newcomer, this practical guide equips you to automate your system’s programs, ensuring a flawless performance each time the curtain rises. So go ahead, empower your server, and let the automation begin! May your servers hum with efficiency and your programs dance in perfect rhythm with every boot. Happy coding!
I love that I can auto start programs now! Thanks!
Fantastic! I’m glad you were able to follow along!
How do you break things down so easily?! This was an awesome read.
Thank you! I really try and imagine I am being shown this for this first time, and write to that.
Some days are better than others 😉