In this section, we will create a script that converts the Nginx daemon to actual system services. This has two functions: the daemon can be controlled using standard commands, and more importantly, it can be automatically started when the system is started and stopped when the system is shut down.
System V scripts
Most Linux-based operating systems use the System-V style init daemon. In other words, their startup process is managed by a daemon called init.
This daemon runs on the principle of runlevel, which represents the state of the computer. Here is a table representing various run levels:
Runlevel
State
0
System is halted
1
Single-user mode (rescue mode)
2
Multiuser mode, without NFS support
3
Full multiuser mode
4
Not used
5
Graphical interface mode
6
System reboot
About init script
An init script (also known as a service startup script or even a sysv script) is a shell script that meets certain standards. The script controls the daemon application through commands such as start, stop, and others. First, when the computer starts, the init daemon will run the script using the start parameter. Another possibility is to execute the script manually from the shell:
[root@example.com ~]# service nginx start
Or if your system does not have a service command:
[root@example.com ~]# /etc/init.d/nginx start
Nginx init script
/etc/init.d/nginx:
Debian-based distribution
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO