SYSTEMD Add custom system Services set custom boot startup methods _linux

Source: Internet
Author: User
Tags documentation

1. Service rights

SYSTEMD has system and user distinction; system (/user/lib/systemd/system/), User (/etc/lib/systemd/user/). A unit file that is created manually by a general system administrator is recommended for storage in/etc/systemd/ system/directory below.

2. Create service files

[Unit]
Description=nginx-high Performance Web server
documentation=http://nginx.org/en/docs/
after= Network.target remote-fs.target nss-lookup.target
 
[Service]
type=forking
pidfile=/run/nginx.pid
execstartpre=/usr/sbin/nginx-t-c/etc/nginx/nginx.conf
Execstart=/usr/sbin/nginx-c/etc/nginx/nginx.conf
execreload=/bin/kill-s HUP $MAINPID
execstop=/bin/ Kill-s QUIT $MAINPID
privatetmp=true
 
[Install]
Wantedby=multi-user.target

[Unit]

Description: A brief description of the service

Documentation: Service Documentation

Before, after: Define the boot sequence. Before=xxx.service, on behalf of this service, starts before Xxx.service starts. After=xxx.service, representing the service started after Xxx.service.

Requires: The unit starts, the unit it needs is also activated, the unit it needs is stopped, and the unit stops.

Wants: Recommended use. The unit is activated, the unit it needs is also activated, and the unit it needs is stopped and has no effect on the unit.

[Service]

Type=simple (default): Systemd that the service will start immediately. The service process will not fork. If the service wants to start another service, do not use this type to start unless the service is a socket-activated type.

Type=forking:systemd that the service started successfully when the service process was fork and the parent process exited. For a regular daemon (daemon), use this type to start up unless you are certain that this startup method does not meet your requirements. Use this startup type to specify pidfile= at the same time in order to systemd the main process that can track the service.

Type=oneshot: This option applies to services that perform only one task and then exit immediately. You may need to set up Remainafterexit=yes so that SYSTEMD still considers the service active after the service process exits.

Type=notify: Same as type=simple, but the engagement service sends a signal to SYSTEMD when it is ready. The implementation of this notification is provided by libsystemd-daemon.so.

Type=dbus: If you start this way, systemd think the service is ready when the specified busname appears on the Dbus system bus.

TYPE=IDLE:SYSTEMD waits for all task (jobs) processing to complete before the idle type of cell is executed. In addition, other acts are similar to Type=simple.

Pidfile:pid file path

Execstart: Specifies the command or script of the startup unit, Execstartpre and Execstartpost sections specify scripts that are executed by the user before or after Execstart. Type=oneshot allows you to specify multiple user-defined commands that you want to execute sequentially.

Execreload: Specifies the command or script to execute when the cell is stopped.

Execstop: Specifies the command or script to execute when the cell is stopped.

Privatetmp:true represents a temporary space for a service to be allocated independently

Restart: If this option is allowed, the process will exit when the service restarts, and the operation will be cleared and restarted through the SYSTEMCTL command.

Remainafterexit: If you set this selection to be true, the service is considered to be active, even if the process has exited, the default value is False, and this option is only required to be configured when Type=oneshot.

[Install]

Alias: Provides an additional name for a space separation for the unit.

Requiredby: The unit is allowed to run a range of dependent units needed, and the Requiredby list obtains dependency information from require.

Wantby: Cells are allowed to run the required weak-dependency units, Wantby obtain dependency information from the want list.

Also: Indicates the unit that is installed or assisted together with the unit.

Defaultinstance: The restriction of the instance unit, which specifies that if the cell is allowed to run the default instance.

3. Heavy Duty Service

Systemctl Enable Nginx.service

A new link to the/usr/lib/systemd/system/nginx.service file will be created in the/etc/systemd/system/multi-user.target.wants/directory.

4. Operation Service

#启动服务
$ sudo systemctl start nginx.service
 
#查看日志
$ sudo journalctl-f-u nginx.service
-logs begin at 42 015-06-25 17:32:20 CST. -
June 10:28:24 Leco.lan systemd[1]: Starting Nginx–high performance Web server ...
June 10:28:24 Leco.lan nginx[7976]: nginx:the configuration file/etc/nginx/nginx.conf syntax is OK
June 10:28:24 L Eco.lan nginx[7976]: nginx:configuration file/etc/nginx/nginx.conf test is successful
June 10:28:24 Leco.lan syste MD[1]: Started Nginx–high performance Web server.
 
#重启
$ sudo systemctl restart Nginx.service
 
#重载
$ sudo systemctl reload Nginx.service
 
#停止
$ sudo systemctl stop Nginx.service

The above is a small set for everyone to bring SYSTEMD Add custom system service settings custom boot Start method all the content, I hope that we support the cloud-Habitat Community ~

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.