creating a service in linux using systemd

vinicenter
2 min readMay 1, 2022

Intro

Creating services is a good thing when you need to run a certain application every time your linux computer boot for example.

Creating a service

At first you need to define your [servicename]. For example, we are using myservice.

And then, you will need to run the following command in your terminal to create your service file (change [servicename] to your service name):

touch /etc/systemd/system/[servicename].service

In my case I am changing [servicename] to myservice:

touch /etc/systemd/system/myservice.service

Now that we have our service file created, let’s put some content inside it.

Run the following command to edit your file (change [servicename] to your service name):

nano /etc/systemd/system/[servicename].service

Inside it you can type this:

[Unit]
Description=
After=network.target

[Service]
Type=simple
ExecStart=
WorkingDirectory=
Restart=on-failure

[Install]
WantedBy=multi-user.target

Configuring your service

You will need to change some configurations in this service file.

  • In Description enter the description of your service.
  • In ExectStart enter the command to start your application.
  • In WorkingDirectory enter the directory you want your application to start.

In my case this is my service file.

[Unit]
Description=Test Service
After=network.target

[Service]
Type=simple
ExecStart=node /home/vinicius/TestApp/app.js
WorkingDirectory=/home/vinicius/TestApp
Restart=on-failure

[Install]
WantedBy=multi-user.target

After that, you can execute the following command to reload all service files to include the new service.

sudo systemctl daemon-reload

Commands to control your service

Run the following command to start the service. (change [servicename] to your service name)

sudo systemctl start [servicename].service

Run the following command to enable the service, so it will start everytime you boot your computer. (change [servicename] to your service name)

sudo systemctl enable [servicename].service

Run the following command to disable the service, so it will not start everytime you boot your computer. (change [servicename] to your service name)

sudo systemctl disable [servicename].service

Run the following command to check the status of your service, so you can debug your application. (change [servicename] to your service name)

sudo systemctl status [servicename].service

And that’s it. you are ready to create your own services.

Happy coding!

--

--