Services

1. Create the Custom Service File

sudo nano /etc/systemd/system/mycustom.service

2. Edit the Service File

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

[Service]
ExecStart=/opt/myscript.sh --arg1 value1 --arg2 value2
Type=simple
Restart=on-failure
StandardOutput=file:/var/log/myscript.log
StandardError=file:/var/log/myscript.log

[Install]
WantedBy=multi-user.target

where:

  • Description=My Custom Script Service: A short text describing what the service does.

  • After=network.target: Ensures the service starts only after the network is up.

  • ExecStart=/opt/myscript.sh --arg1 value1 --arg2 value2: Defines the command (with arguments) that runs when the service starts.

  • Type=simple: Assumes the command runs in the foreground and doesn't fork.

  • Restart=on-failure: Automatically restarts the service if it crashes or exits with an error.

  • StandardOutput=file:/var/log/myscript.log: Redirects standard output (stdout) to a log file (though not always fully supported).

  • StandardError=file:/var/log/myscript.log: Redirects standard error (stderr) to the same log file.

  • WantedBy=multi-user.target: Allows the service to be enabled at boot in multi-user mode (non-GUI).

Configuration options

[Unit]

Defines metadata and ordering dependencies.

Option
Description

Description

A human-readable description (you already have this)

After

Specifies when the unit should start in relation to others (already used)

Requires

Specifies units that must be active for this unit to start

Wants

Like Requires, but failure of these units won’t stop this one

Before

Opposite of After, declares ordering priority

ConditionPathExists

Only start if a specified file exists

Conflicts

If these units are started, this unit is stopped (and vice versa)

[Service]

Core configuration of the service.

Option
Description

ExecStartPre

Commands to run before ExecStart

ExecStartPost

Commands to run after ExecStart

ExecStart

Command that runs when the service starts (must be a full path)

ExecStop

Command to run when stopping the service.

ExecReload

Command for reloading (e.g., re-reading config files)

WorkingDirectory

Set the working directory for the service

User

and Group: Run the service as a specific user or group

Type

Tells systemd how the service starts and runs (e.g., simple, forking)

Environment

Set environment variables

EnvironmentFile

Load environment variables from a file

TimeoutStartSec

How long to wait for ExecStart to finish

TimeoutStopSec

How long to wait before force-stopping

Restart

Defines when the service should restart (e.g., on-failure, always)

RestartSec

Time to wait before restarting

StartLimitBurst

/ StartLimitIntervalSec: Rate-limiting for restarts

KillMode

How processes are killed (e.g., control-group, process)

KillSignal

Signal to send on stop (SIGTERM, SIGKILL, etc.)

StandardInput

Can be tty, file:/path, etc.

StandardOutput

Can be journal, syslog, null, or file:/path (redirection to file not fully supported in all versions of systemd ➡️ Use ExecStart output redirection inside a wrapper script for best reliability)

StandardError

Can be journal, syslog, null, or file:/path

[Install]

Controls enable/disable behavior.

Option
Description

WantedBy

Targets this unit should be enabled for

RequiredBy

Stronger than WantedBy, implies failure if unit fails

3. Reload systemd configuration and start the Service

sudo systemctl daemon-reload
sudo systemctl start myscript.service

4. Enable the Service (Optional)

To start a service automatically on boot, enable it:

sudo systemctl enable myscript.service

5. Check the Status of a service

sudo systemctl status myscript.service

Last updated