In Linux (including Ubuntu), a service is typically a background process that runs continuously to provide specific functionality. These services are managed by a system and service manager like systemd (which is the default in most modern Linux distributions, including Ubuntu).

Creating a Service in Linux (Ubuntu)

To create a service in Ubuntu using systemd, you'll generally follow these steps:

1. Create a Script for Your Service

  • A service in Linux is often a script or program that runs in the background.
  • Write a shell script or executable that defines the behavior of the service. For instance, if you want a simple service to start a Python script, you could create a script like myservice.sh.

Example: myservice.sh

#!/bin/bash
echo "Starting my custom service"
# Add your actual service logic here
# For example, starting a Python script:
# python3 /path/to/your/script.py

Make sure the script is executable:

chmod +x /path/to/myservice.sh

2. Create a Systemd Unit File

  • Systemd uses unit files to manage services. These unit files contain the instructions on how to run, stop, and configure the service.

Create a new .service file in /etc/systemd/system/.

Example: /etc/systemd/system/myservice.service

[Unit]
Description=My Custom Service
After=network.target

[Service]
ExecStart=/path/to/myservice.sh
WorkingDirectory=/path/to/
Restart=always
User=your_user
Group=your_group

[Install]
WantedBy=multi-user.target

Explanation of fields:

  • [Unit]: Contains general information about the service, such as its description and when it should be started.
  • Description: A brief text description of the service.
  • After: Specifies when the service should be started relative to other services (e.g., network.target means after networking is up).
  • [Service]: Specifies how the service runs.
  • ExecStart: The command to run when starting the service (this should be your script or executable).
  • WorkingDirectory: Directory in which the service will run (optional).
  • Restart: Defines what to do when the service exits. always means the service will be restarted if it crashes or stops unexpectedly.
  • User and Group: Optional. Define under which user and group the service should run.
  • [Install]: Describes when the service should be started during boot.
  • WantedBy=multi-user.target: Ensures the service starts in the multi-user runlevel, which is a standard runlevel for most services.

3. Reload Systemd to Register the New Service

Once you have created the .service file, tell systemd to reload its configuration to recognize the new service:

sudo systemctl daemon-reload

4. Enable the Service (Optional)

To make sure the service starts automatically at boot time, use the enable command:

sudo systemctl enable myservice.service

5. Start the Service

Now, you can start the service manually with:

sudo systemctl start myservice.service

6. Check the Status of the Service

You can check whether the service is running properly with:

sudo systemctl status myservice.service

7. Stop the Service

If you need to stop the service, you can run:

sudo systemctl stop myservice.service

8. View Logs (Optional)

If the service is logging output (like from your script), you can use journalctl to view its logs:

sudo journalctl -u myservice.service

9. Disabling and Removing the Service (Optional)

To disable the service from starting at boot:

sudo systemctl disable myservice.service

To remove the service completely, delete the .service file from /etc/systemd/system/ and reload systemd:

sudo rm /etc/systemd/system/myservice.service
sudo systemctl daemon-reload

Conclusion

By following these steps, you can create, configure, and manage custom services on Ubuntu using systemd. This allows you to ensure your custom processes run in the background, restart automatically when they fail, and start on boot.