DEV Community

Cover image for Services in Linux & How to configure your own App as a service?
Yash Raj
Yash Raj

Posted on • Edited on

Services in Linux & How to configure your own App as a service?

in oneline "Services are Linux programs that run in the background" but let's dive into it a little more and find out why it's important, what it does, how can you configure it and what are the commands around it.

In General terms, Services in Linux help you to configure software to run in the background & make sure that they Run all the time automatically when servers are rebooted and as well as follow the right order of startup. when any software that runs as a service in the background is installed they are automatically configured as a service on the system.

Why do we need services?

Services are necessary for various reasons like Continuous operations, Automation, resource management, etc. For example, Services run in the background and operate independently of user interactions. This allows them to provide continuous functionality without any user intervention. Eg:- a web server Software like Apache HTTP can continue to serve web pages to users without needing direct input from users.

Some Basic Commands related to Services

  • To Start a Service:- service <Service name> start or systemctl start <Service name>
  • To Stop a Service:- service <Service name> start or systemctl stop <Service name>
  • Configure a service to start at startup:- systemctl enable <Service name>
  • Configure a service to not start at startup:- systemctl disable <Service name>
  • Check the Status of Service:- systemctl status <Service name> ( for ex.)

A man controlling the Universe with Linux

Configuring your Application as a Service

let's suppose you have an app.py application and you want to configure this as a service & also a service that will automatically start itself when we reboot the system. For this, we have to add a unit file under /etc/systemd/system that will have the same name as the application with an extension service, here it will look like app.service & in this unit file, we have to define some sections under [] bracket like [Service] and provide directives (ex:- Excstart) to them

[Service]
ExcStart = <command used to run the application> #(enough to make app.py a service)

[Install]
WantedBy=multi-user.target #To automatically run when system boots up

Enter fullscreen mode Exit fullscreen mode

and that's it you can use app.py as a service Now. some extra directive which is used generally in a .service unit file.

[Unit]
Description= python application # Meta data about the application

[Service]
ExexStart=<command used to run the application>
ExexStartPre=<path of application> #dependency which is used before running the app.py
ExexStartPost=<path of application> #dependency which is used after running the app.py
Restart=always #restart the application when it crashes

[Install]
WantedBy=multi-user.target

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
alex_gomes_173c598fe10926 profile image
Alex Gomes

Services in Linux & How to Configure Your Own App as a Service

In simple terms, services in Linux are programs that run in the background to handle various tasks automatically. These services are essential for ensuring continuous operation and efficient resource management, running without user interaction. Let's dive into the key aspects of services, why they are important, and how to configure your own application as a service.

For more on how services work and their role in system management, you can also explore related content on my website at cinema Ap

Why Do We Need Services?
Services are crucial for automating tasks, ensuring uptime, and managing resources. They provide continuous functionality and run in the background without requiring user input. For example, web servers like Apache HTTP serve pages to users continuously without direct intervention.

Basic Commands for Managing Services
To Start a Service:

service start

or

systemctl start

To Stop a Service:

service stop

or

systemctl stop

Configure a Service to Start at Boot:

systemctl enable

Configure a Service to Not Start at Boot:

systemctl disable

Check the Status of a Service:

systemctl status

Configuring Your Application as a Service
If you have an application, such as app.py, and want to run it as a service that starts automatically at system boot, follow these steps:

Create a Unit File:

The unit file must be placed under /etc/systemd/system/ with the name app.service. This file defines how the service runs and behaves.

Define the Unit File Structure:

[Unit]: Meta information about the application.

ini
Copy
Edit
[Unit]
Description=Python Application
[Service]: Specifies how the application should run.

ini
Copy
Edit
[Service]
ExecStart=
ExecStartPre= (optional)
ExecStartPost= (optional)
Restart=always # Automatically restart the service if it crashes
[Install]: Specifies that the service should start when the system boots up.

ini
Copy
Edit
[Install]
WantedBy=multi-user.target
In this example, ExecStart= is the key part. Replace with the command you'd typically use to run your Python application (e.g., python3 /path/to/app.py).

Reload Systemd and Enable the Service:

After creating the unit file, reload systemd to apply the changes:

bash
Copy
Edit
systemctl daemon-reload
Enable the service to start at boot:

bash
Copy
Edit
systemctl enable app.service
Start the Service:

Finally, start your service:

bash
Copy
Edit
systemctl start app.service
Now, app.py will run as a service, automatically starting when the system boots up and restarting if it crashes.

By setting up a service, you ensure that your application operates reliably, even during reboots or unexpected crashes, with minimal intervention.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.