Table of Contents
-
- The operating level of the system (pre-knowledge)
- /etc/init.d
-
- About directory /etc/rc.d/init.d
- /etc/rc.local
- The difference between /etc/init.d and /etc/rc.local
- systemctl
- Summarize
- Notice
`/etc/init.d` or `/etc/rc.local` or `systemctl` can be started
The operating level of the system (pre-knowledge)
Level | Remarks |
---|---|
Run level 0 | System shutdown status, the system default run level cannot be set to 0, otherwise it cannot start normally |
Run level 1 | Single-user working status, root authority, for system maintenance, remote login is prohibited |
Multi-user state (no network) | |
Run level 3 | Complete multi-user state (with network), enter the console command line mode after login |
Run level 4 | The system is not used, keep |
Run level 5 | X11 console, after login Enter the graphical GUI mode |
Run level 6 | The system is normally shut down and restarted, the default run level cannot be set to 6, otherwise it cannot start normally |
/etc/init.d
The directory /etc/init.d/ contains many system service start and stop scripts, which control various affairs. Of course, /etc/init.d/ is far from simple.
When you look at the /etc/ directory, you will find directories of the form rc0-6.d (rc0.d, rc1.d…rc6.d), which contain many scripts that control the process. These scripts start with “K” or “S”, with scripts starting with “K” running before scripts starting with “S”. Where these scripts are placed determines when the script will start running.
/etc/init.d/command options
command
is the command to run, and the options can be as follows
start stop reload restart force-reload for example: /etc/init.d/network stop or: /etc/init.d/network restart
Commonly used scripts under the directory /etc/init.d/
are
Of course, you can also have more scripts, depending on your system version and adding them yourself.
/etc
directory display (only relevant directories, files and links are displayed):
etc: init.d: -- init.d -> rc.d/init.d functions mst mysql -- mysql -> /usr/local/mysql5.7/mysql.server netconsole network rc0.d: -- rc0.d -> rc.d/rc0.d K50netconsole -- K50netconsole -> ../init.d/netconsole K90network -- K90network -> ../init.d/network rc1.d: -- rc1.d -> rc.d/rc1.d K50netconsole -- K50netconsole -> ../init.d/netconsole K90network -- K90network -> ../init.d/network rc2.d: -- rc2.d -> rc.d/rc2.d K50netconsole -- K50netconsole -> ../init.d/netconsole K90network -- K90network -> ../init.d/network rc3.d: -- rc3.d -> rc.d/rc3.d K50netconsole -- K50netconsole -> ../init.d/netconsole K90network -- K90network -> ../init.d/network rc4.d: -- rc4.d -> rc.d/rc4.d K50netconsole -- K50netconsole -> ../init.d/netconsole K90network -- K90network -> ../init.d/network rc5.d: -- rc5.d -> rc.d/rc5.d K50netconsole -- K50netconsole -> ../init.d/netconsole K90network -- K90network -> ../init.d/network rc6.d: -- rc6.d -> rc.d/rc6.d K50netconsole -- K50netconsole -> ../init.d/netconsole K90network -- K90network -> ../init.d/network rc.d: init.d: functions mst mysql -- mysql -> /usr/local/mysql5.7/mysql.server netconsole network rc0.d: rc1.d: rc2.d: rc3.d: rc4.d: rc5.d: rc6.d: rc.local rc.local -- rc.local -> rc.d/rc.local
From the directory structure, /etc/init.d
is a symbolic link file, which is actually linked to the directory /etc/rc.d/init.d
About directory /etc/rc.d/init.d
In most Linux distributions, the service under the directory
/etc/rc.d/init.d
is started, and the actual service script is stored in this directory.
For each operating level of Linux, there is a subdirectory under /etc/rc.d/ which is rc0.d, rc1.d … rc6.d. In fact, the files stored in these rcN.d/ directories are linked to the script link files under the directory /etc/rc.d/init.d/. The services to be executed at each level are in the corresponding directory. For example, the services to be started at level 5 are all placed under rc5.d/, but all the links placed under this rc5.d/ are linked to the corresponding actual script files in /etc/rc.d/init.d/.
For example, the S90network link file under the directory /etc/rc.d/rc2.d/ is actually linked to the script file network under the directory /etc/rc.d/init.d/.–Introduction to /etc/init.d/ of the Linux directory
/etc/rc.local
/etc/rc.local
is a script file, as can be seen from the above /etc file path, it is linked to /etc/rc.d/rc.local
. It is executed once in run level 3, read and executed line by line according to the script command, and the system user logs in after the execution is completed once at startup.
The difference between /etc/init.d and /etc/rc.local
/etc/init.d | /etc/rc.local |
---|---|
This is a folder, which contains shell scripts, and the head of the script needs to define the startup level | This is a file, which stores the path of the shell script to be executed |
Generally, it is the startup that comes with rpm installation , stop, restart and other functional scripts | Generally user-defined scripts |
High execution authority, with 0-6 levels | Can only be executed after execution authority 3 |
Scripts of the same level starting with K run before scripts beginning with S | Execute the commands in the file in order, and the execution order is related to the line where the command is located |
systemctl
systemctl is a systemd tool mainly responsible for controlling the systemd system and service manager.
- Write system service files
# vi /etc/systemd/system/myapp.service ---------------------------------------------------- #If /etc/systemd/system/myapp.service has been modified and needs to be updated, execute the following command #systemctl daemon-reload # Check if the service is correctly identified #systemctl list-unit-files grep myapp [Unit] #Service description, write meaningful content for easy identification Description=myapp service [Service] Type=simple #Set the working directory of the application WorkingDirectory=/myapp_path ExecStart=/myapp_path/startMyApp.sh ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target
- Check if the system service file is recognized
systemctl list-unit-files|grep myapp -------------------------- myapp.service disabled
- Write a program startup script
# vi /myapp_path/startMyApp.sh ---------------------------------------------------- #!/bin/sh #The above line is very important, it will report 203 error #It is best to cd to the program directory first cd /myapp_path #Use the absolute path of java. If the java executable program cannot be found, a 203 error will also be reported /usr/java/jdk1.8/bin/java -jar myapp.jar
Set execution permissions
chmod +x myapp.sh
- service operation
# start the service systemctl start myapp.service # Out of service systemctl stop myapp.service # start at boot systemctl enable myapp.service systemctl list-unit-files grep myapp -------------------------- myapp.service enabled # Turn off autostart systemctl disable myapp.service systemctl list-unit-files grep myapp -------------------------- myapp.service disabled
- 203 errors that may be encountered
If the path of the startup script specified by the ExecStart parameter is incorrect, a 203 error will be prompted. (Just correct it.)
If no script parser is specified in the launcher script (.sh file), a 203 error will be displayed. (add at the beginning of the file: #!/bin/sh)
The startup program script (.sh file) does not have execution permission, and a 203 error will be prompted. (chmod +x myapp.sh)
The service file is a service file that is unique to Linux systems that use systemd as the initialization program. It is called a “service hive file” and is used to replace the script files in the old initialization system, but they may exist in the system at the same time.
If they exist at the same time, the script files in the directory /etc/init.d/ will have higher priority than the service files in the directory /etc/systemd/system/.
Summary
It is recommended to use systemctl for management script operations. For example, after Redis is compiled and installed, run install_server.sh directly (there is this file in the Redis directory, look for it carefully), and the service will be generated by itself, and start the service with commands such as service redis service name restart
(or systemctl restart redis service name
).
*
and the boot self-startup is recommended to be written in the rc.local
script file, the purpose of doing this is to facilitate the management of self-startup items.
Pay attention to the sentence # Please note that you must run 'chmod + x /etc/rc.d/rc.local' to ensure
on line 10, not only chmod + x /etc/rc.local
but also chmod + x /etc/rc.d/rc.local
, so that the self-starting script has execution permission. In addition, I personally tested, do not use the nohup
command in the rc.local script, otherwise it will kill the relevant process. I haven’t done too much understanding of the specific reasons. Interested friends can talk about it in the comments. For example, for some jar programs, just java -jar xxx.jar
, or /usr/bin/su - root -c "some script"
like the author.
Note
The above script files, whether they are .sh files or link files, require execution permission
chmod 755 *.sh
or chmod + x *.sh
The above script files, whether they are .sh files or link files, require execution permission
chmod 755 *.sh
or chmod + x *.sh
The above script files, whether they are .sh files or link files, require execution permission
chmod 755 *.sh
or chmod + x *.sh