Use systemctl to add custom system services on CentOS 7

Source: Internet
Author: User

CentOS 7 inherits the new features of RHEL 7, such as powerful systemctl. The use of systemctl also makes the/etc/init of system services in the past. d's startup script method has changed, greatly improving the efficiency of system service execution. However, the service configuration is significantly different from the previous one. To be honest, it is much simpler and easier to use.


In the following example, we use forever to implement automatic startup of the Node. js project to explore systemctl of CentOS 7.


Prerequisites: the Node. js environment has been configured successfully, and the forever package has been successfully installed. There is a Node. js program that can be run.


The systemctl script of the CentOS 7 service is stored in:/usr/lib/systemd/. There are systems and users, such as programs that can be executed without logon, there is still a system service, that is, under the/usr/lib/systemd/system folder.

Each service. the end of the service is usually divided into three parts: [Unit], [Service], and [Install]. The service I wrote is used to run Node at startup. js project. The details are as follows:

Knife unitshortdescriptionappsxiyoulibapiafterappsnetwork.tar get remote-fs.target nss-lookup.target [Service] Type = forkingPIDFile =/node. js/pidExecStart =/usr/local/bin/forever start/node. js/xiyoulib/bin/wwwExecReload =/usr/local/bin/forever restart/node. js/xiyoulib/bin/wwwExecStop =/usr/local/bin/forever stop/node. js/xiyoulib/bin/wwwPrivateTmp = true uninstall install?wantedby=multiouser.tar get

The [Unit] section mainly describes the service, including Description and After. Description is used to describe the service, and After is used to describe the service category.

[Service] is the key of the Service. It is the setting of the number of workers executed in detail by the Service. Here, Type = forking is the form of execution in the background, and PIDFile is the file path where the PID is stored, execStart is the detailed execution command of the service, ExecReload is the restart command, and ExecStop is the stop command. PrivateTmp = True indicates that an independent temporary space is allocated to the service. Note: absolute paths are required for all commands for starting, restarting, and stopping [Service]. An error is returned when relative paths are used!

[Install] is related to the service installation settings, which can be set to multiple users.


After the service script is compiled according to the above, it is saved in the/usr/lib/systemd/system folder with 754 permissions, then systemctl can be used for configuration.

First, use systemctl start [service name (also file name)] to test whether the service can be successfully executed. If it cannot be executed, use systemctl status [service name (also file name)] check the error information and other service information, and modify the information based on the error report until start is enabled. If you are not at ease, you can run the restart and stop commands again.

Then, you only need to use systemctl enable xxxxx to add the written service to the startup of the machine.


My script compilation method is based on the nginx compiling method, and can also be based on programs with similar functions.


In this case, although systemctl is more unfamiliar than init. d. That method is much simpler and easy to use. There are a lot of operations that can be simplified by systemctl, and now there are a lot of materials, it seems that RHEL/CentOS is more advanced than other Linux distributions, and this update eventually discards the Linux 2.6 kernel, improving both speed and stability.


Reprinted please indicate the source: http://blog.csdn.net/yuanguozhengjust/article/details/38019923

Use systemctl to add custom system services on CentOS 7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.