您的当前位置:首页 --> CentOS入门 |
centos 7.x编写开机启动服务 |
浏览次数:495 关键词 ( centos ) |
centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。关于Systemd的详情介绍在这里。 Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动,如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令 1 systemcel enable nginx.service
设置开机启动即可。 1.在系统服务目录里创建nginx.service文件 1 vim /lib/systemd/system/nginx.service内容如下 1 2 3 4 5 6 7 8 9 10 11 12 13 [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
[Unit]:服务的说明 保存退出。 2.设置开机启动 1 systemctl enable nginx.service
3.其它命令 设置开机自启动 1 systemctl enable nginx.service停止开机自启动 1 systemctl disable nginx.service查看服务当前状态 1 systemctl status nginx.service重新启动服务 1 systemctl restart nginx.service查看所有已启动的服务 1 systemctl list-units --type=service4.Systemd 命令和 sysvinit 命令的对照表
Sysvinit 命令 |
Systemd 命令 |
备注 |
service foo start
systemctl start foo.service
用来启动一个服务 (并不会重启现有的)
service foo stop
systemctl stop foo.service
用来停止一个服务 (并不会重启现有的)。
service foo restart
systemctl restart foo.service
用来停止并启动一个服务。
service foo reload
systemctl reload foo.service
当支持时,重新装载配置文件而不中断等待操作。
service foo condrestart
systemctl condrestart foo.service
如果服务正在运行那么重启它。
service foo status
systemctl status foo.service
汇报服务是否正在运行。
ls /etc/rc.d/init.d/
systemctl list-unit-files –type=service
用来列出可以启动或停止的服务列表。
chkconfig foo on
systemctl enable foo.service
在下次启动时或满足其他触发条件时设置服务为启用
chkconfig foo off
systemctl disable foo.service
在下次启动时或满足其他触发条件时设置服务为禁用
chkconfig foo
systemctl is-enabled foo.service
用来检查一个服务在当前环境下被配置为启用还是禁用。
chkconfig –list
systemctl list-unit-files –type=service
输出在各个运行级别下服务的启用和禁用情况
chkconfig foo –list
ls /etc/systemd/system/*.wants/foo.service
用来列出该服务在哪些运行级别下启用和禁用。
chkconfig foo –add
systemctl daemon-reload
当您创建新服务文件或者变更设置时使用。
telinit 3
systemctl isolate multi-user.target (OR systemctl isolate runlevel3.target OR telinit 3)
改变至多用户运行级别。
5.Sysvinit 运行级别和 systemd 目标的对应表
Sysvinit 运行级别 |
Systemd 目标 |
备注 |
0
runlevel0.target, poweroff.target
关闭系统。
1, s, single
runlevel1.target, rescue.target
单用户模式。
2, 4
runlevel2.target, runlevel4.target, multi-user.target
用户定义/域特定运行级别。默认等同于 3。
3
runlevel3.target, multi-user.target
多用户,非图形化。用户可以通过多个控制台或网络登录。
5
runlevel5.target, graphical.target
多用户,图形化。通常为所有运行级别 3 的服务外加图形化登录。
6
runlevel6.target, reboot.target
重启
emergency
emergency.target
紧急 Shell
|
下一条 centos系统中网络配置相关 上一条 大名鼎鼎的Root,这些你都知道吗? |
数千企业与站长的选择,7*24小时稳定运行
了解更多套餐详细