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


修改系统启动级别

旧版

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

新版

修改默认启动级别为3

systemctl enable multi-user.target

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

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

若修改默认启动级别为5,需要将之前的启动级别disable

systemctl disable multi-user.target

该命令实际删除了软链接default.target

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

然后再启用启动级别5

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的一个初步印象,需要一个逐步适应的过程,希望大家多多讨论交流。