一、服务分类:
1、RPM包默认安装服务(systemctl启动)
1)独立服务
2)xinetd
2、源码包安装的服务(绝对路径启动)
二、6版本
RPM包服务
1、独立服务
启动
1)/etc/init.d/ 执行文件
/etc/init.d/httpd start|stop|restart
2)service
service httpd start|stop|restart
自启动
1)chkconfig自启动管理
chkconfig --list服务 查看服务是否开启
chkconfig --level 2345 服务 on/off 命令管理自启动
系统级别:0关机 1单用户 2功能缺失 3字符界面 4保留级别 5图形化 6重启
runlevel 查看当前系统级别 3 5 上次3,当前5
init 切换系统级别
2)/etc/rc.d/rc.local 编辑文件(开机执行命令的配置文件,7版本默认无x权限)
vim /etc/rc.d/rc.local 写入 /etc/init.d/httpd start|stop|restart
3)ntsysv 图形化管理
2、基于xinetd服务(非重点)
启动
1)/etc/xinetd.d/
如:vim /etc/xinetd.d/rsync
disable=yes改成no 重启服务service xinetd restart
自启动
1)chkconfig自启动管理
2)ntsysv 图形化管理
3、源码包安装的服务
启动
1)源码包启动脚本
服务路径 start|stop|restart
2)添加service管理
安装服务
ps:make -j 4 4个CPU同时使用
ln -s /usr/local/apache2/bin/apachectl /etc/init.d/apache2
自启动
1)/etc/rc.d/rc.local 编辑文件
vim /etc/rc.d/rc.local 写入 路径 start|stop|restart
2)添加checkconfig管理:
编辑配置文件:vim /etc/init.d/apache
查看开启、停止顺序是否占用:ls /etc/rc5.d/
#chkconfig:35 86 76
#description:source packageapache
chkconfig --add apache2
chkconfig --list apache2
三、7版本
1、systemd的配置文件位置:
/usr/lib/systemd/system/:服务启动脚本存放位置
/run/systemd/system/:系统执行过程中产生的服务脚本
/etc/systemd/system/:管理员根据自己主机系统的需求所创建的执行脚本,操作系统启动后到底会不会执行某些服务其实是看/etc/systemd/system/目录的
2、systemctl管理服务:
基本上服务的管理都是通过systemctl命令来完成的
systemctl:管理服务状态,开机是否启动等
格式:systemctl [选项] [执行服务的守护进程名称]
选项:
start:启动服务
stop:停止服务
restart:重启
reload:不关闭服务的情况下,重新读取服务配置文件
enable:开机启动
disable:开机不启动
status:查看指定服务状态
is-enabled:查看指定服务是否为开机启动enabled启动/disable不启动
systemctlstop atd.service
后缀有以下几种:
.service:用来启动和控制守护进程和进程的服务单元
.target:执行环境类型,也就是启动流程中的multi-user.target和graphical.target,相当于对服务和启动事项的一个分组
查看系统上所有服务:
命令:systemctl
选项:
list-units:显示当前启动unit,添加--all选项则列出启动和未启动的所有unit
list-unit-files:显示/usr/lib/systemd/system/的unit状态
3、切换操作环境:
在centos7中虽然还是可以使用init * 的命令,但是已经没有了运行级别的概念,这种启动/运行级别的概念转变成了切换操作环境,那么我们
可以切换的操作环境有:
1.graphical.target:图形化界面
2.multi-user.target:命令行模式
3.rescue.target:救援模式
4.emergency.target:紧急处理系统的错误,需要使用root登录,再无法使用rescue.target的情况下可以尝试使用此模式
5.shutdown.target:关机
4、使用systemctl管理源码包nginx:
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx #服务描述
After=network.target #表示服务在network服务启动后启动
[Service]
Type=forking #表示后台运行模式
ExecStart=/usr/local/nginx/sbin/nginx #服务启动脚本位置
ExecReload=/usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
ExecStop=/usr/local/nginx/sbin/nginx -s stop #停止服务
[Install]
WantedBy=multi-user.target #表示此unit依附于multi模式
最后执行:systemctl daemon-reload #重新加载
systemctl 管理Nginx
vim /usr/lib/system
四、6版本与7版本对比
1、systemd代替init(系统内核PID1)
2、6版本启动service自启动chkconfig 7启动自启动都是systemctl
3、B服务依赖A服务,systemctl会自动启动A
4、/etc/rc.d/rc.local 7版本默认无执行权限