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:
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:
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:
To start the service, run the following command:
To stop the service, run the following command:
To restart the service, run the following command:
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.
This script sets up a PM2 service that can be controlled with the following commands:
Note that you need to place this script in the
/etc/init.d/ directory
and make it executable by runningchmod +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.