Centos 7.x write boot Service

Source: Internet
Author: User

Centos 7.x write boot Service

Centos 7 and later use Systemd for system initialization. Systemd is the latest initialization system (init) in Linux. Its main design goal is to overcome the inherent shortcomings of sysvinit, increase the system startup speed. Here is the detailed introduction of Systemd.

The Systemd service file ends with. service. For example, if you want to create nginx for start-up, if you use the yum install command to install it, the yum command automatically creates the nginx. service file and directly uses the command

1 systemcel enable nginx. service

Set boot start.
Here I use the source code for compilation and installation, so I need to manually create the nginx. service file.
Programs that can run without logging on to the system are stored in the system service, namely:

1/lib/systemd/system/

1. Create the nginx. service file in the system service directory.

1vim/lib/systemd/system/nginx. service

The content is as follows:

12345678910111213 [Unit] Description = nginx after1_network.tar get [Service] Type = forking ExecStart =/usr/local/nginx/sbin/nginx ExecReload =/usr/local/nginx/sbin/nginx- s reload ExecStop =/usr/local/nginx/sbin/nginx-s quitPrivateTmp = true [Install] wantedby=multiouser.tar get

[Unit]: Service Description
Description: describes the service.
After: Describes the service category.
[Service] Service running parameter settings
Type = forking is the form of background running
ExecStart is the specific running command of the service.
ExecReload is the restart command
ExecStop is the Stop command
PrivateTmp = True indicates that an independent temporary space is allocated to the service.
Note: the absolute path is required for the [Service] startup, restart, and stop commands.
[Install] The service installation settings at the running level can be set to multiple users, that is, the system running level is 3.

Save and exit.

2. Set startup

1 systemctl enable nginx. service

3. Other commands
Start the nginx Service

1 systemctl start nginx. service

Set auto-start

1 systemctl enable nginx. service

Stop Auto-start

1 systemctl disable nginx. service

View Current Service Status

1 systemctl status nginx. service

Restart the service

1 systemctl restart nginx. service

View all started services

1 systemctl list-units -- type = service

4. Comparison between Systemd and sysvinit

Sysvinit commandSystemd commandRemarksService foo startsystemctl start foo. service is used to start a service (which does not restart the existing one) service foo stopsystemctl stop foo. service is used to stop a service (which does not restart the existing one ). Service foo restartsystemctl restart foo. service is used to stop and start a service. When service foo reloadsystemctl reload foo. service is supported, reload the configuration file without interrupting the waiting operation. Service foo condrestartsystemctl condrestart foo. service restart the service if it is running. Service foo statussystemctl status foo. service reports whether the service is running. Ls/etc/rc. d/init. d/systemctl list-unit-files-type = service is used to list services that can be started or stopped. Chkconfig foo onsystemctl enable foo. the service sets the service to enable chkconfig foo offsystemctl disable foo at the next startup or when other triggering conditions are met. the service sets the service to disable chkconfig foosystemctl is-enabled foo at the next startup or when other triggering conditions are met. service is used to check whether a service is enabled or disabled in the current environment. Chkconfig-listsystemctl list-unit-files-type = service output the service enabling and disabling at each running level chkconfig foo-listls/etc/systemd/system /*. wants/foo. service is used to list the running levels at which the service is enabled and disabled. Chkconfig foo-addsystemctl daemon-reload is used when you create a new service file or change the settings. Telinit 3 systemctl isolate multi-user.target (OR systemctl isolate runlevel3.target OR telinit 3) changed to multi-user run level.

5. Table corresponding to the Sysvinit running level and systemd target

Sysvinit running levelObjective of SystemdRemarks0runlevel0.tar get and poweroff.tar get disable the system. 1, s, singlerunlevel1.target, rescue.tar get single-user mode. 2, 4runlevel2.tar get, runlevel4.target, multi-user.target user-defined/domain-specific runtime level. The default value is 3. 3runlevel3.tar get, multi-user.target multi-user, non-graphical. You can log on through multiple consoles or networks. 5 runlevel5.tar get, graphical.tar get multi-user, graphical. Generally, all running level 3 services are added with graphical logon. 6runlevel6.tar get, reboot.targetrestart emergencyemergency.tar get emergency Shell

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.