Page Content

Tutorials

How To Create A Startup Service In Linux Using Systemd

Create a startup service in linux

The standard, contemporary method for managing Linux background processes is to create a systemd service. It guarantees that your program or script launches automatically upon bootup, restarts in the event of a crash, and accurately logs output.

This is a detailed guide on how to put one up.

Create your Service File

Systemd looks for user-defined services in /etc/systemd/system/. You’ll need sudo privileges to create a file here.

Example: sudo nano /etc/systemd/system/myapp.service

Also Read About What Is The Difference Between Systemd And Systemctl?

Define the Configuration

Paste the following template into your file. For this example, let’s assume you’re running a Python script:

Ini, TOML

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

[Service]
ExecStart=/usr/bin/python3 /home/user/scripts/myscript.py
WorkingDirectory=/home/user/scripts
StandardOutput=inherit
StandardError=inherit
Restart=always
User=username

[Install]
WantedBy=multi-user.target

Analysis of Important Fields:

  • After=network.target: Makes sure the service doesn’t start until the network is up.
  • ExecStart: Your script’s absolute path to the executable. Use absolute paths at all times.
  • Restart=always: Attempts to restart the service automatically in the event that it fails.
  • User: The particular user account that should execute the process; for security reasons, try to avoid running as root.

Also Read About What Is GRUB Bootloader? Linux Boot Process And Commands

Register and Start the Service

Once the file is saved, you need to tell Linux it exists and set it to run at boot.

  1. Reload the daemon to pick up the new file: sudo systemctl daemon-reload
  2. Enable the service (this ensures it starts on boot): sudo systemctl enable myapp.service
  3. Start it now: sudo systemctl start myapp.service

Managing Your Service

Now that it’s running, you can manage it like any other system process:

ActionCommand
Check Statussudo systemctl status myapp.service
Stop Servicesudo systemctl stop myapp.service
Restart Servicesudo systemctl restart myapp.service
View Logsjournalctl -u myapp.service -f

How to check startup services in linux

How to check startup services in linux
How to check startup services in linux

Depending on whether you want to see what is currently running or what is scheduled to run at boot, there are a few go-to commands. Since most modern Linux distributions (Ubuntu, Fedora, Debian, CentOS) use systemd, systemctl will be your primary tool.

List All Enabled Services

To see every service that is configured to start automatically when the system boots, use:

Bash

systemctl list-unit-files --type=service --state=enabled
  • STATE enabled: Means it will start on boot.
  • STATE disabled: Means it stays off unless started manually or by another process.

See What is Currently Running

If you want to see which services are active and running right now:

Bash

systemctl list-units --type=service --state=running

The “Master List” (Status & Description)

For a comprehensive view that shows the load status, active state, and a short description of every service on the system:

Bash

systemctl --type=service

Also Read About Linux Boot Process Step By Step And Interview Questions

Analyzing Boot Performance

Sometimes you want to check startup services because the computer is booting slowly. Linux has a built-in “blame” tool for this:

  • Total Boot Time: systemd-analyze
  • Service Rankings: systemd-analyze blame(This lists services in order of how long they took to initialize, which is great for troubleshooting lag.)

Legacy and Alternative Methods

Depending on your setup, some services might be managed differently:

Crontabs

Some “startup” scripts are actually hidden in cron jobs. Check for tasks set to run at reboot:

crontab -l | grep "@reboot"

The GUI Way (Desktop Users)

If you are using a desktop environment like GNOME or KDE, there is often a “Startup Applications” app.

  • Command: gnome-session-properties (for GNOME/Ubuntu users)

Summary Table

GoalCommand
See what starts at bootsystemctl list-unit-files --state=enabled
See what’s running nowsystemctl list-units --state=running
Find slow startup servicessystemd-analyze blame
Check a specific servicesystemctl status <service_name>

Startup services in linux command line

Managing services in Linux is primarily handled by systemd, which uses the systemctl command. Think of it as the mission control for your starts, stops, and keeps an eye on everything running in the background.

The Essentials: Managing Service State

These are the commands you’ll use most often to control a service (like nginx, docker, or ssh) in real-time.

  • Start a service: sudo systemctl start <service_name>
  • Stop a service: sudo systemctl stop <service_name>
  • Restart a service: sudo systemctl restart <service_name> (Stops and starts it again)
  • Reload configuration: sudo systemctl reload <service_name> (Keeps the service running but updates settings)

Also Read About What Are The Runlevels In Linux? & Common Systemd Targets

Automation: Boot-up Behavior

Just because a service is running now doesn’t mean it will start automatically after a reboot. You have to tell Linux your preference.

  • Enable at boot: sudo systemctl enable <service_name>
  • Disable at boot: sudo systemctl disable <service_name>
  • Check boot status: systemctl is-enabled <service_name>

Monitoring & Troubleshooting

If something goes wrong, these commands act as your diagnostic toolkit.

  • Check status: systemctl status <service_name>This provides a “health report,” showing if the service is active, its PID (Process ID), and the most recent log entries.
  • List all active services: systemctl list-units --type=service
  • View logs (Journal): journalctl -u <service_name>Use -f (e.g., journalctl -u nginx -f) to watch logs in real-time as you debug.

The Old School: service and init.d

On older systems (or if you have muscle memory from a decade ago), you might see the service command. While most modern distributions redirect this to systemctl automatically, it looks like this:

  • sudo service <service_name> start
  • sudo service <service_name> status

Summary Table

ActionCommandPersistent across Reboot?
Start Nowsystemctl startNo
Stop Nowsystemctl stopNo
Auto-startsystemctl enableYes
Prevent Auto-startsystemctl disableYes

Also Read About What Is Kernel Loading In Linux? Kernel Loading Process

How to disable startup services in linux

To disable a service from starting automatically when your Linux system boots, you primarily use systemd. Disabling a service is different from stopping it; a “disabled” service won’t start on its own at boot, but it can still be started manually later.

The Standard Way: systemctl

Most modern Linux distributions (Ubuntu, Fedora, Debian, CentOS, Arch) use systemctl.

Step-by-Step Disable

  1. Stop the service first (if it’s currently running):sudo systemctl stop <service_name>
  2. Disable the service:sudo systemctl disable <service_name>
  3. Verify the change:systemctl is-enabled <service_name>This should return disabled.

The “Nuclear” Option: Masking

Sometimes a service is “pulled in” by another service even if you disable it. To prevent a service from being started by anything (manually or by another process), use mask:

  • sudo systemctl mask <service_name>
  • To undo this later, use unmask.

Managing GUI Startup Apps

If you are looking to disable applications that pop up when you log into your desktop (like Discord, Steam, or Slack), the command line systemctl won’t usually find those. Those are managed via .desktop files.

  • Location: Look in ~/.config/autostart/
  • How to disable: You can delete the file or move it out of that folder:mv ~/.config/autostart/app-name.desktop ~/.config/autostart/app-name.desktop.bak

Finding What to Disable

Not sure which services are slowing down your boot time? Use these diagnostic commands:

  • List all enabled services:systemctl list-unit-files --state=enabled
  • See which services take the longest to boot:systemd-analyze blameThis lists services in descending order of how much time they added to your startup.

Summary of Commands

GoalCommand
Stop current auto-startsudo systemctl disable <service>
Stop and Disable at oncesudo systemctl disable --now <service>
Prevent any/all startingsudo systemctl mask <service>
Check boot speed impactsystemd-analyze blame

Also Read About Linux Installation Step By Step For Beginners Complete Guide

Hemavathi
Hemavathihttps://govindhtech.com/
Myself Hemavathi graduated in 2018, working as Content writer at Govindtech Solutions. Passionate at Tech News & latest technologies. Desire to improve skills in Tech writing.
Index