Running a Service in the Background with SystemV in Linux: A Comprehensive Guide

Published on

Introduction

SystemV, also known as System Five, is an init system used in Unix-like operating systems, including many Linux distributions. It is responsible for initializing the system, starting and stopping services, and managing runlevels.

In this blog, we'll discuss how to run a service in the background using SystemV init scripts.

Writing a SystemV Init Script

An init script is a shell script that is executed at system startup to initialize the system and start or stop services. In SystemV, init scripts are stored in the /etc/init.d directory.

To write a SystemV init script, you need to follow a specific format, including a few required and optional sections.

The required sections are the following:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          service_name
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the service_name service
### END INIT INFO

In this section, you need to replace service_name with the name of your service and provide a short description of what the service does. The Required-Start and Required-Stop fields specify the dependencies of the service, and the Default-Start and Default-Stop fields specify the runlevels at which the service should be started and stopped.

The next section of the init script is the actual logic to start and stop the service:

start() {
  # Start the service
  /path/to/service start
}

stop() {
  # Stop the service
  /path/to/service stop
}

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

exit 0

In this section, you need to replace /path/to/service with the actual path to your service, and replace service_name with the name of your service.

The start and stop functions contain the commands to start and stop the service, respectively. The case statement is used to handle the start, stop, and restart options, as specified on the command line.

Installing and Starting the Service

To install the service, make the init script executable and copy it to the /etc/init.d directory:

chmod +x /path/to/service_name
cp /path/to/service_name /etc/init.d/

To start the service, run the following command:

service service_name start

To stop the service, run the following command:

service service_name stop

To restart the service, run the following command:

service service_name restart

Example of Running PM2 with SystemV

This is how I am using SystemV for running PM2, a popular process manager for Node.js applications, in the background.

#!/bin/bash

# chkconfig: 2345 99 01
# description: PM2 is a process manager for Node.js applications.

. /etc/init.d/functions

PM2_HOME="/root/.pm2"
PM2_BIN="$(which pm2)"

start() {
    echo "Starting PM2"
    if [ -f "$PM2_HOME/pm2.pid" ]; then
        echo "PM2 is already running"
        exit 1
    fi
    $PM2_BIN start
}

stop() {
    echo "Stopping PM2"
    $PM2_BIN stop
}

restart() {
    echo "Restarting PM2"
    $PM2_BIN restart
}

status() {
    $PM2_BIN list
}

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

exit $?

This script sets up a PM2 service that can be controlled with the following commands:

sudo service pm2 start
sudo service pm2 stop
sudo service pm2 restart
sudo service pm2 status

Note that you need to place this script in the /etc/init.d/ directory and make it executable by running chmod +x /etc/init.d/pm2.

Conclusion

In conclusion, SystemV is an important init system used in many Linux distributions for managing system initialization, starting and stopping services, and managing runlevels. With the steps outlined in this blog, you now know how to write a SystemV init script, install and start the service, and control its behavior.

By following these steps, you can run a service in the background with SystemV in Linux, ensuring that it remains active even when you log out of the system.

Updates straight in your inbox!

A periodic update about my life, recent blog posts, TIL (Today I learned) related stuff, things I am building and more!

Share with others

Liked it?

Tags

Views

You may also like

  • linux

    Effortlessly Manage Torrent Downloads with Headless qBittorrent on Linux

    Learn how to install qBittorrent on a Linux server without a GUI and run it in the background with SystemD for a seamless torrenting experience.

    2 min read
  • linux

    Keep Your Services Running in the Background with SystemD

    Do you want your Linux services to keep running even after you log out of your system? SystemD makes it easy to run services in the background. In this guide, we'll show you how to create a SystemD service, start and stop it, and monitor its status.

    3 min read
  • linux

    Init Systems Unveiled — Understanding the Differences between SystemD and SystemV

    Get a comprehensive understanding of SystemD and SystemV init systems in Linux. Explore the similarities and differences between these two init systems, with explanations to help you make an informed decision.

    3 min read