Linux Systemctl is a collection of system Management daemons, tools, and libraries that supersede system V, service, and Chkconfig commands, and the initial process is primarily responsible for controlling SYSTEMD systems and service managers. Through Systemctl–help can see the command is mainly divided into: query or send control commands to the SYSTEMD service, the Management Unit service commands, service files related commands, tasks, environment, snapshot related commands, SYSTEMD service configuration overload, system to open the machine related commands.
1. List all available units
# Systemctl List-unit-files
2. List all running units
# Systemctl List-units
3. List all failed units
# systemctl–failed
4. Check if a cell (such as Crond.service) is enabled
# Systemctl is-enabled Crond.service
5. List all services
# Systemctl List-unit-files–type=service
6. How to start, restart, stop, reload, and check the status of services (such as Httpd.service) in Linux
# Systemctl Start Httpd.service
# systemctl Restart Httpd.service
# Systemctl Stop Httpd.service
# Systemctl Reload Httpd.service
# SYSTEMCTL Status Httpd.service
Note: When we use Systemctl's start,restart,stop and reload commands, the terminal does not output anything, only the status command can print the output.
7. How to activate the service and enable or disable the service at power on (i.e., start the Mysql.service service automatically when the system starts)
# Systemctl Is-active Mysql.service
# Systemctl Enable Mysql.service
# systemctl Disable Mysql.service
8. How to block (let it not start) or display services (such as Ntpdate.service)
# Systemctl Mask Ntpdate.service
Ln-s '/dev/null '/etc/systemd/system/ntpdate.service '
# Systemctl unmask Ntpdate.service
RM '/etc/systemd/system/ntpdate.service '
9. Kill the service using the SYSTEMCTL command
# Systemctl Kill Crond
10. List all system mount points
# Systemctl List-unit-files–type=mount
11. Mount, Unmount, re-mount, reload the system mount point and check the status of the mount point in the system
# Systemctl Start Tmp.mount
# Systemctl Stop Tmp.mount
# systemctl Restart Tmp.mount
# Systemctl Reload Tmp.mount
# SYSTEMCTL Status Tmp.mount
12. Activate, enable, or disable mount points at startup (automatically mount when system starts)
# Systemctl Is-active Tmp.mount
# Systemctl Enable Tmp.mount
# systemctl Disable Tmp.mount
13. Block (Let it not enable) or visible mount point in Linux
# Systemctl Mask Tmp.mount
Ln-s '/dev/null '/etc/systemd/system/tmp.mount '
# Systemctl unmask Tmp.mount
RM '/etc/systemd/system/tmp.mount '
14. List all available system socket interfaces
# Systemctl List-unit-files–type=socket
15. Check all configuration details for a service
# systemctl Show MySQL
16. Get a list of dependencies for a service (HTTPD)
# Systemctl List-dependencies Httpd.service
17. Activate Rescue mode
# Systemctl Rescue
18. Enter Emergency mode
# SYSTEMCTL Emergency
19. List the currently used run level
# Systemctl Get-default
20. Start the operation level 5, that is, the graphics mode
# Systemctl Isolate Runlevel5.target
Or
# Systemctl Isolate Graphical.target
21. Start Run Level 3, which is multi-user mode (command line)
# Systemctl Isolate Runlevel3.target
Or
# Systemctl Isolate Multiuser.target
22. Set the default run level for multi-user mode or graphics mode
# Systemctl Set-default Runlevel3.target
# Systemctl Set-default Runlevel5.target
23. Restart, stop, suspend, hibernate the system or bring the system into mixed sleep
# Systemctl reboot
# Systemctl Halt
# Systemctl Suspend
# Systemctl Hibernate
# Systemctl Hybrid-sleep
For those who do not know what the operating level is, explain the following.
Runlevel 0: Shutting down the system
Runlevel 1: Rescue, Maintenance Mode
Runlevel 3: Multi-user, no graphics system
Runlevel 4: Multi-user, no graphics system
Runlevel 5: Multi-user, graphical system
Runlevel 6: Close and restart the machine
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=xiyoulibapi
After=network.target Remote-fs.target Nss-lookup.target
[Service]
Type=forking
Pidfile=/node.js/pid
Execstart=/usr/local/bin/forever start/node.js/xiyoulib/bin/www
Execreload=/usr/local/bin/forever restart/node.js/xiyoulib/bin/www
Execstop=/usr/local/bin/forever stop/node.js/xiyoulib/bin/www
Privatetmp=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.
CentOS 7 Systemctl