Create a Service

Systemd

Systemd services are defined in unit files with the .service extension. Here's a basic structure:

[Unit]
Description=Your service description
After=network.target

[Service]
Type=simple
User=your_username
WorkingDirectory=/path/to/service/directory
ExecStart=/path/to/service/executable

[Install]
WantedBy=multi-user.target

Replace placeholders: Description: Short description of your service. After: Specify dependencies (e.g., network services). User: User running the service. WorkingDirectory: Service's working directory. ExecStart: Path to the service executable. WantedBy: Target unit to enable automatic startup.

Save the file: Save it as [service_name].service in /etc/systemd/system/.

Reload and enable:

sudo systemctl daemon-reload
sudo systemctl enable <service_name>.service

Start the service:

sudo systemctl start <service_name>.service.

SysV

SysV services are typically shell scripts located in /etc/init.d/. Here's a basic example:

#!/bin/bash

start() {
  # Start your service here
  /path/to/service/executable &
}

stop() {
  # Stop your service here
  pkill -f /path/to/service/executable
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit 0

Make the script executable:

chmod +x /etc/init.d/<service_name>

Start/stop the service:

/etc/init.d/<service_name> start/stop

Optional: Enable automatic startup by linking the script to /etc/rc?.d/S (depending on your runlevel).

Upstart

Upstart uses job files with the .conf extension usually located in /etc/init.

Here's a basic example:

description "Your service description"

start on started network-interfaces
script
  # Start your service here
  /path/to/service/executable &
end script

pre-stop script
  # Stop your service here
  pkill -f /path/to/service/executable
end script

Save the file: Save it as [service_name].conf in /etc/init/.

Reload and start:

sudo initctl reload 
sudo start <service_name>

Last updated

Was this helpful?