[LINUX] Use the Service service to boot from the startup script or command

1. Sysinit initialization mode

        Under Linux, if you want to implement a custom script or command that starts automatically at boot, if the system is initialized in Sysinit mode , you can add the script to be executed at the end of the rc.local file in the /etc/ directory . Execute the script at startup

./home/localhost/test.sh

        Then use the command:

        Add the following snippet at the end:

         It should be noted that the added instruction needs to be before exit 0 :

2. Systemd initialization mode

        Sometimes we find that there is no rc.local file under /etc/, then we need to consider that the installed linux may be in systemd initialization mode . The following three files exist in Systemd initialization mode:

  • /etc/systemd/system

  • /usr/lib/systemd/system

  • /lib/systemd/system

        The function of these three files is to read the configuration file in the above path, start the service and execute the program when starting the operating system. The priority is as follows:

         According to the above description, the path /etc/systemd/system is a system-level file directory, storing some installation units deployed by system administrators (root, etc.), with the highest priority, and /usr/lib/systemd/system is a user-level file Directory, our custom service is generally stored in this directory. If there are services with the same name in the three directories at the same time, they will be overwritten according to the order of priority at startup.

3. Write a service custom self-starting file

        We create a new file name with the suffix .service under the path /etc/systemd/system, taking the remote connection service ssh.service as an example, the format is as follows:

        The Service file mainly consists of three parts: [Unit], [Service] and [Install] .

        The common fields of [Unit] are described as follows: 

        Commonly used fields under [Service] are as follows:

       Commonly used fields under  [Install] are as follows:

Fourth, start the service service

         After customizing the service file, execute the following commands in the console to activate or deactivate the service:

Guess you like

Origin blog.csdn.net/qq_41884002/article/details/128017661