In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article shares the configuration and use of systemctl for you. The article covers the configuration and use of systemctl. I hope you can get something through this article.
# # introduction
Systemd controls boot service, boot level and other functions by managing unit.
The / usr/lib/systemd/system directory contains various unit files, service unit with the service suffix, boot-level unit with the target suffix, and so on. Here are the files about the service suffix. Because systemd in order to perform self-boot is controlled by the unit of these *. Service, and the service is divided into system service (system) and user service (user).
Profile description:
All * .service files are stored in the / lib/systemd/system directory
We can look at the crontab.service file and see what's written in it.
[root@zhangsf system] # cd ~ [root@zhangsf ~] # cat / usr/lib/systemd/system/ crond.service [Unit] Description=Command SchedulerAfter=auditd.service systemd-user-sessions.service time- sync.target [service] EnvironmentFile=/etc/sysconfig/crondExecStart=/usr/sbin/crond-n $CRONDARGSExecReload=/bin/kill-HUP $MAINPIDKillMode= process [install] WantedBy=multi-user.target
It can be seen that it is roughly divided into the following parts
* [Unit] blocks: startup sequence and dependencies
Description: a brief introduction to current services Documentation: where documents are used After: if auditd.service or systemd-user-sessions.service services need to be started, Before: crontd services should be started before this service: crontd services should be started before those services
Note: After and Before only involve startup sequence, not dependencies.
[Service] initiating behavior
Startup command > ExecStart field: define the command executed when starting the process > ExecReload field: command executed when restarting the service > ExecStop field: command executed when the service is stopped > ExecStartPre field: command executed before starting the service > ExecStartPost field: command executed after starting the service > ExecStopPost field: command executed after stopping the service Note: all startup settings can be preceded by a hyphen (-) Means "suppress error", that is, when an error occurs, it does not affect the execution of other commands. For example, EnvironmentFile=-/etc/sysconfig/sshd (note the hyphen after the equal sign) means that even if the / etc/sysconfig/sshd file does not exist, an error will not be thrown. Note: the start, restart, and stop commands in [Service] all require absolute paths! The startup type Type field defines the startup type. The values it can set are as follows: > simple (default): the process started in the ExecStart field > forking:ExecStart field will start in the fork () mode, in which case the parent process will exit and the child process will become the main process (running in the background) > oneshot: similar to simple, but only executed once, Systemd will wait for it to finish execution before starting other services > dbus: similar to simple, but will start after waiting for the D-Bus signal > notify: similar to simple A notification signal will be sent after startup, and then Systemd will start other services > idle: similar to simple, but will not start the service until all other tasks have been executed. One use case is to allow the output of the service not to mix with the output of other services the restart behavior Service block has some fields that define the restart behavior: > KillMode field: define how Systemd stops the sshd service: > control-group (default): all child processes in the current control group will be killed > process: kill only the main process > mixed: the main process will receive the SIGTERM signal The child process receives the SIGKILL signal > none: no process will be killed, just execute the stop command of the service. > Restart field: the restart method of Systemd after sshd exit is defined. In the above example, Restart is set to on-failure, which means that if any unexpected failure occurs, sshd will be restarted. If sshd stops normally (such as executing the systemctl stop command), it will not restart. > > the Restart field can be set as follows. > > no (default): no restart after exit > > on-success: restart only when normal exit (exit status code is 0) > > on-failure: restart only when abnormal exit (exit status code is non-0), including signal termination and timeout > > on-abnormal: restart only when the signal is terminated and timed out > > on-abort: only when the uncaptured signal is terminated Always: no matter what the reason for exiting, always restart. Note: for daemons, it is recommended to set it to on-failure. For services that allow error exit, you can set it to on-abnormal. RestartSec field: indicates the number of seconds to wait before Systemd restarts the service. The above example is set to wait 42 seconds.
[install] how to install this configuration file
WantedBy field: indicates the Target where the service is located.
Target means a service group, which represents a set of services.
WantedBy=multi-user.target means: the Target where sshd is located is multi-user.target.
This setting is important because when you execute the systemctl enable sshd.service command, a symbolic link to sshd.service is placed in the multi-user.target.wants subdirectory under the / etc/systemd/system directory.
Systemd has a default startup Target.
Systemctl get-default# output multi-user.target
The above results indicate that the default startup Target is multi-user.target. All services in this group will be booted. This is why the systemctl enable command can set boot up.
The systemctl list-dependencies and systemctl isolate commands are also useful when using Target.
# viewing all the services included in multi-user.target systemctl list-dependencies multi-user.target# switching to another target#shutdown.target is the shutdown status systemctl isolate shutdown.target
Generally speaking, there are two commonly used Target:
Multi-user.target: indicates multi-user command line statu
Graphical.target: represents the status of the graphical user, which is dependent on multi-user.target.
# # examples
[root@zhangsf system] # cat / usr/lib/systemd/system/node- exporter.service[ Unit] Description=This is prometheus node exporterAfter= docker.service[ service] Type=simpleExecStart=/usr/local/bin/node_exporterExecReload=/bin/kill-HUP $MAINPIDKillMode=processRestart=on-failure [Install] WantedBy=multi-user.target
Execute a command
Systemctl daemon-reloadsystemctl enable node-exporter.servicesystemctl start node-exporter.service
View the log
[root@zhangsf system] # tail-f / var/log/messagesFeb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "- sockstat" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "- stat" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11 : 53 level=info msg= "- textfile" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "- time" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "- timex" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_ Exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "- uname" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "- vmstat" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "- xfs" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "- zfs" source= "node_exporter.go:104" Feb 6 11:53:40 zhangsf node_exporter: time= "2020-02-06T11:53:40+08:00" level=info msg= "Listening on: 9100" source= "node_exporter.go:170"
After reading this article, have you learned to configure and use systemctl? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.