Author:
Joel Gray
Published: 27 September 2022
Read Time: ~5 minutes
Run Anything as a Service on Linux
Do you have a home Minecraft server that you shuts down every time your SSH session disconnects? Or do you have some custom python scripts you want to run all the time?
Let’s learn about systemctl services…
The following command will show you all the current services that are running along with a brief description of what their purpose is.
$ sudo systemctl list-units
The image above is an example of that output of the ‘list-units’ command, we can see universal kernel services as well as some common services for web servers such as ‘apache2.service’. You can even see custom services like my twitter bot service ‘fuelBotTwitter.service’ at the very bottom.
I’m going to show you how to set up your own service like this!
But first, a few handy commands!
Check the status of a service
I’m going to show you some useful commands that you’ll need to know and apply them to my apache2 service, which is actually the service hosting this very website. First lets check the status of this service.
$ sudo systemctl status apache2.service
The image above is typically what you’ll see when you check the status of a service, it’ll tell you if the service is active, some stats on the job and also a brief logging history at the bottom. All very handy.
Restart a service
If you’re having issues with a service or have made changes to it’s configuration since you last started the service, you can use the restart service command below. If you don’t get any output from the command it means the service has been restarted successfully.
$ sudo systemctl restart apache2.service
View running logs of a service
But what happens if your service doesn’t restart correctly? How do I know what’s wrong? That’s where the journalctl command comes in! The command below using the -u flag to specify what unit or service we wish to view logs for and specifies the -f flag to follow the log in real time to view any error logs that may be coming through.
$ sudo journalctl -u apache2.service -f
View all logs of a service
But you may want to view logs in the past. You can do this by viewing all logs for that service by just leaving out the -f flag. Although this often spits out far too much data for us.
$ sudo journalctl -u apache2.service
View recent logs of a service
So what we can do is specify how far back in the logs we want to look – the command a fairly self explanatory here.
$ sudo journalctl -u apache2.service --since "1 hour ago"
OR
$ sudo journalctl -u apache2.service --since "10 minutes ago"
Enable/Start a service
If you’ve just created a new service and wish to be able to use it, you need to first enable the service and then start the service. This can be done with these commands: (We’re still using apache2 as an example it can be any service)
$ sudo systemctl enable apache2.service
OR
$ sudo systemctl start apache2.service
Disable/Stop a service
Finally, if you wish to stop a service or permanently disable a service you can do so which the following commands:
$ sudo systemctl stop apache2.service
OR
$ sudo systemctl disable apache2.service
Creating Your Own Custom Linux Service
Firstly you need some sort of script, program, process or server you wish to running constantly as a service. I’m going to show you how I set my python twitter bot program as a linux service using systemctl.
Create your service config file
Go to the correct directory for storing service config files and create a new file with the name of your service/app as the same and suffix the file with ‘.service’. Example below:
$ cd /etc/systemd/system
$ nano service-name.service
Next we need to include the relevant information that systemctl/systemd needs to interpret our service and be able to run it correctly. Here is an example of my python twitter bot service config file. Note here that I’m using a python virtual environment as my executor, you can use your name system python install or even things like bash instead!
Below is the contents of my twitterBot.service file. Change the ExecStart paths to your interpreter and programs to suit your needs and then save the file. To save and close the file, press Ctrl+x then type ‘y’ and press enter.
[Unit]
Description=Monitor Service for fuelBotTwitter
[Service]
ExecStart=/home/joel/fuelBotTwitter/twitterBotVenv/bin/python3 /home/joel/fuelBotTwitter/bot.py
Restart=always
EnvironmentFile=/etc/environment
[Install]
WantedBy=multi-user.target
You can do some background reading here, if you wish.
Okay let’s talk a little about the attributes in this file.
Description: | Brief description of the service, will show in list-units command |
ExecStart: | Parameters for starting the service, first is the interpreter/runner and second is the program or script to be run. |
Restart: | Tell linux you always want to restart the service in the event of a failure, no matter the reason for the service failure |
EnvironmentFile: | A file where we can specify environment variables for the service, set to /etc/environment by default |
WantedBy: | Is necessary to be set for your system to enable the service successfully. If you omit this the command ‘systemctl enable myservice.service’ will fail. |
Start your service
Now it’s time to start your service using some of the commands we looked at in the first half of this blog.
First we need to reload the systemctl deamon to have it recognise our new myservice.service file.
$ sudo systemctl daemon-reload
Next we need to enable the service to have it start every time we boot
$ sudo systemctl enable myservice.service
Then we want to start the service to have it be operational immediately.
$ sudo systemctl start myservice.service
And finally we want to check the status of the service just to be sure it’s active and running correctly.
$ sudo systemctl status myservice.service
If you have any issues with this you can use the journalctl commands mentioned earlier, check the logs and do some research to try sus it out. If not you can drop me a comment or an email. Hope this helps!
Written by Joel Gray
27/09/2022
Checkout some of our other popular blog posts
Are you sick of adding “python -m” to the beginning of your pip install...
wpDiscuz