Add custom system services with Systemctl on CentOS 7

Source: Internet
Author: User

CentOS 7 inherits the new features of Rhel 7, such as the powerful systemctl, while the use of SYSTEMCTL has changed the way in which the previous system services/ETC/INIT.D startup scripts, and greatly improves the efficiency of system services. But the configuration of services and the past has also undergone a great difference, to say the truth, changed to simple and easy to use a lot.

Below we use forever to realize the node. JS Project self-Starting as an example, the first approach to CentOS 7 systemctl.

Prerequisite: the node. JS Environment has been successfully configured, the Forever package is successfully installed, and there is a node. JS program that can run.

CentOS 7 Service Systemctl script is stored in:/usr/lib/systemd/, the system and the user (users) of the points, such as the need to boot without logging on to run the program, or the existence of system services, that is:/usr/lib/ Under the Systemd/system directory

Each service ends with a. Service and is typically divided into 3 parts: [Unit], [service], and [Install], which I wrote to run the node. JS project, as follows:

[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 is primarily a description of the service, including description and after,description for describing the service, after which is used to describe the service category

The [Service] section is the service of the key, is the service of some specific operating parameters of the settings, here type=forking is the form of the background run, Pidfile for the file path to store the PID, Execstart for the service of the specific Run command, Execreload Restart command, execstop for the Stop command, Privatetmp=true to assign a separate temporary space to the service, note: The [Service] section of the start, restart, Stop command all require the use of absolute path, the use of relative path will be error!

The [Install] section is a set of settings for a service installation that can be set to a multi-user

After the service script is written as above, it is saved in the/usr/lib/systemd/system directory with 754 permissions, and can be configured with Systemctl

First, use SYSTEMCTL start [service name (also file name)] To test whether the service can run successfully, and if not, you can use the SYSTEMCTL status [service name (also file name)] To view error messages and other service information, and then modify them according to the error. Until you can start, you can also test the restart and stop commands if you are not confident.

Next, simply use the Systemctl enable XXXXX to add the services you have written to the boot boot.

My scripting methods refer to Nginx's writing methods, and can also be based on programs similar to other functions.

So it seems, although Systemctl is unfamiliar, but in fact than init.d that way a lot simpler, and the use of simple, systemctl can simplify the operation there are many, now there are a lot of information, it seems rhel/centos than other Linux distribution is more advanced , the update also finally abandoned the Linux 2.6 kernel, both speed and stability have improved a lot.

Add custom system services with Systemctl 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.