Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compare the Service Management between CentOS 6 and CentOS 7

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to compare the service management of CentOS 6 and CentOS 7. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Centos7 and centos6 are different from initialization technology, service startup, boot file and so on. Let's share the service management comparison between centos7 and centos6.

1. Introduction to sysvinit, upstart, systemd / CentOS 5CentOS 6CentOS 7 remarks sysvinit ✅✅✅ first generation, traditional, most compatible (/ etc/init.d/, / etc/rc.X) upstart ❌✅❌ second generation, similar to systemd prototype (/ etc/init) systemd ❌❌✅ third generation, with cgroup Systemd takes over the entire system (/ usr/lib/systemd) 2. Sysvinit, upstart, Systemd common command action sysvinitupstartsystemd view service mytest statusinitctl status mytestsystemctl status mytest.service start service mytest startinitctl start mytestsystemctl start mytest.service close service mytests topinitctl stop mytestsystemctl stop mytest.service forcible kill process kill-9 PIDkill-9 PIDsystemctl kill mytest.service-signal=9 restart service mytest restartinitctl restart mytestsystemctl restart mytest.service restart service mytest reloadinitctl reload mytestsystemctl reload mytest.service boot chkconfig mytest on/etc/init/mytest.conf configure start on runlevel [3] systemctl enable mytest.service3. Runlevel CentOS 6CentOS 70haltrunlevel0.target-> poweroff.target1Single user moderunlevel1.target-> rescue.target2Multiuser, without NFSrunlevel2.target-> multi-user.target3Full multiuser moderunlevel3.target-> multi-user.target4unusedrunlevel4.target-> multi-user.target5X11runlevel5.target-> graphical.target6rebootrunlevel6.target-> reboot.target View cat / etc/inittabsystemctl get-default boot effective edit / etc/inittabsystemctl set-default multi-user.target switch init 5systemctl isolate graphical.target4 immediately. Log query

CentOS 6: manual grep in / var/log/messages, / var/log/dmesg, / var/log/secure, troublesome and inefficient

CentOS 7: using journalctl uniformly, multiple factors can be used to match, such as time period, service name, log level, and so on. In addition, systemd logs are compressed by default and are binary files and cannot be viewed directly

Journalctl commands function CentOS 6 to compare all journalctl logs, including system, kernel, etc. Manually check the current boot log in the corresponding log file grepjournalctl-dmesg dmesgjournalctl-boot to check the current boot time, and then check the boot time / boot. Journalctl-boot=-1 looks at the last log query launched between the last boot and the current boot time, and then cat / var/log/... Journalctl-since= "2018-08-01 12:00:00" View the log from the specified time to the current log manually in the log grepjournalctl-since=yesterday-until=today view the log at 0-24:00 yesterday manually in the log grepjournalctl-n 20 view the last 10 lines tail-n 20journalctl-f real-time scroll display the latest log tail-fjournalctl-e directly turn to the last tailjournalctl-u mytest.service to view the specified service log first query the log saving path Then cat view journalctl-p 0 to view the specified log level, log level from 0 to 7 through syslog put different levels of log into different files journalctl-u mytest.service-o json-pretty or-o verbose view each log details (including meta-information) no journalctl-disk-usage view log disk space du-shx / var/log/messages, etc. 5. Implement the daemon

CentOS 6

Sysvinit needs to be implemented by itself.

Nohup &

Screen

Supervisor

Upstart is similar to systemd, just run the program in the foreground.

CentOS 7

Start by systemd and run the program in the foreground.

6. Examples of sysvinit, upstart, systemd

Sysvinit

Cat > / etc/init.d/mytest

Upstart

Cat > / etc/init/mytest.conf

Systemd

Cat > / usr/lib/systemd/system/mytest.service7. PID management

Sysvinit: a PID file needs to be generated for later shutdown, restart, etc.

Upstart: no PID file is needed. Upstart records the main process ID, but the child process ID does not.

Systemd: no PID file is required. All process ID is taken over by cgroup.

8. Built-in resource restrictions

CentOS 6: there is no easy way to restrict process resources other than ulimit

CentOS 7: in addition to ulimit, it also supports some cgroup restrictions, such as memory restrictions and cpu resource restrictions on processes.

[Service] ExecStart=...MemoryLimit=500MCPUShares=100

In addition, CentOS 7 can view the performance data in cgroup through the systemd-cgtop command

9. Automatic restart of service exception

Upstart

Start on runlevel [3] description "mytest" exec / root/mytest.shpost-stop exec sleep 5respawnrespawn limit unlimited

Systemd

[Unit] Description= mytest[Service] Type=simpleExecStart=/root/mytest.shRestart=alwaysRestartSec=5StartLimitInterval= 0[Install] WantedBy=multi-user.target

Both of the above two methods indicate that you can restart automatically for unlimited times and wait 5 seconds before each restart.

10. Log writing mode

CentOS 6: export to a file yourself or record through syslog (such as the logger command)

CentOS 7: as long as the program is started by systemd, simply log the output to standard output or standard error

It is recommended that centos7 only output some meta-information of the application to standard output or standard error, such as successful startup, startup failure, etc.

Exporting business logs to journal is not recommended. Because all logs in journal exist in one file, it can cause 2 problems:

If log persistence is not done, it is stored in memory by default, which will result in up to half of the memory being occupied.

The storage capacity is so large that it is time-consuming to query other logs.

Solution: export to syslog, [Service] support StandardOutput=syslog

11. Specify the level of each log

CentOS 6: output different levels of logs to different files through syslog

CentOS 7: just add at the beginning of each line of the output, such as

Echo 'hello, emerg'echo' hello, alert'echo 'hello, crit'echo' hello, err'echo 'hello, warning'echo' hello, notice'echo 'hello, info'echo' hello, debug'12. Systemd log is saved permanently

Systemd logs are saved in memory by default, so when the server is restarted, the previous logs cannot be viewed through journalctl. Solution:

Mkdir-p / var/log/journalsystemctl restart systemd-journald after reading the above, do you have any further understanding of how to compare the service management of CentOS 6 and CentOS 7? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report