初次体验CentOS 7的systemd

新发布的CentOS 7 中使用systemd服务代替了以前版本的SysV服务,对比下两种启动方式的不一样。bash


修改系统启动级别less

旧版
dom

编辑配置文件/etc/inittab,设置启动级别为3 (多用户文字界面),修改initdefault前面的数字为3,保存重启ide

新版spa

修改默认启动级别为3rest

systemctl enable multi-user.target

这个命令实际则是在目录 /etc/systemd/system 下建立了一个软连接code

ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'

若修改默认启动级别为5,须要将以前的启动级别disableorm

systemctl disable multi-user.target

该命令实际删除了软连接default.targetserver

rm '/etc/systemd/system/default.target'

而后再启用启动级别5ip

systemctl enable graphical.target

实际建立了新的软连接

ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'

固然你也能够跳过命令直接以建立软连接的方式来改变启动级别


应用程序的自启动项

当经过yum安装了httpd服务后,准备将其添加到自启动项里,

旧版

chkconfig httpd on

新版

systemctl enable httpd

实际是建立了软连接

ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

关掉httpd的自启动

systemctl disable httpd

实际软连接被删除

rm '/etc/systemd/system/multi-user.target.wants/httpd.service'

关于启动级别3下面的服务启动项都在/etc/systemd/system/multi-user.target.wants目录下,而级别5的则在目录graphical.target.wants下面

咱们打开这个连接文件 httpd.service 能够看到内容,就是有关httpd的启动脚本

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target


关于服务的启动/关闭/重启

旧版

service httpd {start|stop|restart}

新版

systemctl {start|stop|restart} httpd

查看当前httpd的运行状态

systemctl status httpd

输出结果

httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Thu 2014-07-17 15:12:50 CST; 4s ago
  Process: 2762 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 2769 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ?..2769 /usr/sbin/httpd -DFOREGROUND
           ?..2770 /usr/sbin/httpd -DFOREGROUND
           ?..2771 /usr/sbin/httpd -DFOREGROUND
           ?..2772 /usr/sbin/httpd -DFOREGROUND
           ?..2773 /usr/sbin/httpd -DFOREGROUND
           ?..2774 /usr/sbin/httpd -DFOREGROUND
Jul 17 15:12:50 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
Jul 17 15:12:50 localhost.localdomain httpd[2769]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the ...his message
Jul 17 15:12:50 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full

查看全部服务自启动状态

旧版

chkconfig --list

新版

systemctl list-unit-files


以上是我对systemd的一个初步印象,须要一个逐步适应的过程,但愿你们多多讨论交流。

相关文章
相关标签/搜索