HomeLinuxEasy methods to Create a Systemd Service Unit in Linux

Easy methods to Create a Systemd Service Unit in Linux


Systemd is a contemporary software program suite that gives many elements on a Linux system together with a system and repair supervisor. It’s appropriate with SysV and LSB init scripts and works as a alternative for sysvinit.

A systemd service is outlined in a unit file (a unit is a illustration of a service and system assets corresponding to gadgets, sockets, mount factors, and many others.). Customized service unit information ought to be saved within the /and many others/systemd/system/ listing and should have an .service extension. For instance, a customized test-app service makes use of /and many others/systemd/system/test-app.service unit file.

A unit file is a plain textual content ini-style file that often contains three widespread sections. The primary part is often the Unit part which carries generic details about the unit that’s not depending on the kind of unit.

The subsequent part is the unit kind part, for a service, it’s a Service part. And the ultimate part is the Set up part which carries set up info for the unit.

On this information, we’ll present the way to create a brand new systemd service and handle the service utilizing the systemctl command, in Linux.

Creating Customized Systemd Service File in Linux

To run an software or program or script as a service underneath systemd, you may create a brand new systemd service as follows. Begin by creating the service unit file named test-app.service (keep in mind to switch test-app with the precise identify of your service or software), underneath /and many others/systemd/system/:

# vi /and many others/systemd/system/test-app.service

The next configuration is used to outline a service for working a Flask software utilizing Gunicorn, a Python WSGI HTTP Server for UNIX.

[Unit]
Description=Gunicorn daemon for serving test-app
After=community.goal

[Service]
Consumer=root
Group=root
WorkingDirectory=/apps/test-app/
Setting="PATH=/apps/test-app/bin"
ExecStart=/apps/test-app/bin/gunicorn --workers 9  -t 0  --bind 127.0.0.1:5001 -m 007 wsgi:app --log-level debug --access-logfile /var/log/gunicorn/test_app_access.log --error-logfile /var/log/gunicorn/test_app_error.log
ExecReload=/bin/kill -s HUP $MAINPID
RestartSec=5

[Install]
WantedBy=multi-user.goal

Let’s briefly describe every configuration directive within the configuration above:

  • Description – is used to specify an outline for the service.
  • After – defines a relationship with a second unit, the community.goal. On this case, the test-app.service is activated after the community.goal unit.
  • Consumer – is used to specifying the person with whose permissions the service will run.
  • Group – is used to specify the group with whose permissions the service will run.
  • WorkingDirectory – is used to set the working listing for executed processes.
  • Setting – is used to set atmosphere variables for executed processes.
  • ExecStart – is used to outline the instructions with their arguments which are executed when this service is began.
  • ExecReload – is used to outline the instructions to execute to set off a configuration reload within the service.
  • WantedBy – allows a symbolic hyperlink to be created within the .needs/ or .requires/ listing of every of the listed unit(s), multi-user.goal on this case, when the test-app.service unit is enabled utilizing the systemctl allow command.

You will discover all service unit configuration parameters, nicely described within the Service unit configuration documentation.

Save the unit file and shut it. Then reload systemd with this new service unit file by working:

# systemctl daemon-reload command

Bear in mind to all the time run this command after enhancing a unit file.

Handle Systemd Service in Linux

To begin/activate the service, run the systemctl command as follows:

# systemctl begin test-app.service

To examine if the service is working or not, concern the systemctl command as proven.

# systemctl standing test-app.service
Check Status of Service
Verify the Standing of the Service

To allow the service to start out at system boot, use the systemctl allow command. You possibly can examine if the service has been enabled utilizing the systemctl is-enable command as follows:

# systemctl allow test-app.service
# systemctl is-enabled test-app.service
Enable Service to Start at Boot
Allow Service to Begin at Boot

Alternatively, you can even allow and begin the service concurrently proven.

# systemctl allow --now test-app.service

To cease/deactivate the service, run the systemctl cease command as follows:

# systemctl cease test-app.service

To restart the service, run the systemctl restart command as follows:

# systemctl restart test-app.service

It’s also possible to disable a service to forestall it from beginning at system boot, utilizing the systemctl disable command. You possibly can examine if the service has been enabled utilizing the systemctl is-enable command as follows:

# systemctl disable test-app.service
# systemctl is-disabled test-app.service

Alternatively, you may disable and cease it concurrently proven.

# systemctl disable --now test-app.service

For extra particulars about managing systemd companies and different assets, run:

# man systemctl

If You Admire What We Do Right here On TecMint, You Ought to Contemplate:

TecMint is the quickest rising and most trusted neighborhood website for any form of Linux Articles, Guides and Books on the net. Hundreds of thousands of individuals go to TecMint! to go looking or browse the hundreds of revealed articles out there FREELY to all.

If you happen to like what you’re studying, please think about shopping for us a espresso ( or 2 ) as a token of appreciation.

Support Us

We’re grateful in your by no means ending help.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments