Use systemctl to add custom system services on centos 7

Source: Internet
Author: User

Centos 7 inherits 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 operating efficiency of system services. However, the service configuration is significantly different from the previous one. To be honest, it is much simpler and easier to use.


The following example describes how to use forever to implement automatic startup of node. js project and how to use systemctl of centos 7.


Prerequisites: The node. js environment has been configured successfully. After the forever package is successfully installed, a node. js program 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 run without logon, there is still a system service, that is, under the/usr/lib/systemd/system directory.

Each service. the end of a service is generally divided into three parts: [unit], [SERVICE], and [install]. The service I wrote is used to start a node. JS project, details:

[Unit]Description=xiyoulibapiAfter=network.target 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 [Install]WantedBy=multi-user.target

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 and the setting of some specific running parameters of the service. Here, type = forking is the form of running in the background, and pidfile is the file path for storing the PID, execstart is the specific running command of the service, execreload is the restart command, execstop is the Stop command, and privatetmp = true indicates that the service is allocated an independent temporary space. Note: the absolute path is required for all the commands for starting, restarting, and stopping [SERVICE]. An error is returned if the relative path is used!

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


After the service script is compiled as above, it is saved in the/usr/lib/systemd/system directory with 754 permissions. Then you can use systemctl for configuration.

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

Then, you only need to use systemctl enable XXXXX to add the Written service to the on-premises startup.


The Compiling Method of my script is based on the compiling method of nginx, or similar programs with other functions.


In this case, although systemctl is unfamiliar, it is actually better than init. d. That method is much simpler and easier to use. There are still many operations that can be simplified by systemctl. Now there are a lot of information. It seems that RHEL/centos is more advanced than other Linux releases, this update finally abandoned 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.