chkconfig --list #列出全部的系统服务。CentOS6以前 systemctl list-units --all --type=service #查看全部服务。CentOS6以后 systemctl list-units --type=service #查看全部已经启动的服务。CentOS6以后
systemctl enable servicename systemctl start servicename
在 /etc/rc.d/rc.local
或者其连接文件 /etc/rc.local
文件中添加启动命令html
在CentOS7以后可能没法自启动nginx
CentOS7以后采用systemd做为init,rc.local
也是经过systemd
的service启动的。redis
输入vim
cat /usr/lib/systemd/system/rc-local.service
查看其启动配置centos
# This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # This unit gets pulled automatically into multi-user.target by # systemd-rc-local-generator if /etc/rc.d/rc.local is executable. [Unit] Description=/etc/rc.d/rc.local Compatibility ConditionFileIsExecutable=/etc/rc.d/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.d/rc.local start TimeoutSec=0 RemainAfterExit=yes
其中发现启动rc.local
是经过命令 ExecStart=/etc/rc.d/rc.local start
实现的,所以 /etc/rc.d/rc.local
必须得有执行权限。网络
若其没有执行权限,应给予。socket
chmod +x /etc/rc.d/rc.local
以后则须要启动rc-local.service
服务ide
systemctl enable rc-local.service systemctl start rc-local.service
若未能成功启动ui
systemctl enable rc-local.service The unit files have no [Install] section. They are not meant to be enabled using systemctl. Possible reasons for having this kind of units are: 1) A unit may be statically enabled by being symlinked from another unit's .wants/ or .requires/ directory. 1) A unit's purpose may be to act as a helper for some other unit which has a requirement dependency on it. 1) A unit may be started when needed via activation (socket, path, timer, D-Bus, udev, scripted systemctl call, ...).
提示启动service里面没有install这节的内容。那就给它经过多用户的target启动就能够了。this
vim /usr/lib/system/system/rc-local.service [Install] WantedBy=multi-user.target
而后再次enable启动服务:
systemctl enable rc-local.service ln -s '/lib/systemd/system/rc-local.service' '/etc/systemd/system/multi-user.target.wants/rc-local.service'
以上解决方法摘录自网络,systemd的原理我并不了解
启动脚本能够写成服务而后用systemd添加到自启动,不必定非要添加到rc.local
此方法貌似是sysv的init使用,systemd行不行有待试验
将脚本拷贝至 /etc/init.d/
路径下,或者建立须要的脚本文件便可
以后增长可执行权限
chmod +x /etc/init.d/scriptname.sh
最后添加自启动
chkconfig --add scriptname.sh chkconfig scriptname.sh on
最后一步是否是能够用systemctl enable scripname.sh
我没试过
查到还有一种方法是先在/etc/profile.d/
建立脚本以后添加可执行权限再拷贝至/etc/init.d/
,以后的流程相同。
chkconfig方法的参数设置
#!/bin/sh 代表此脚本使用/bin/sh来解释执行 #chkconfig: 2345 80 90 2345表示系统运行级别 0——关机, 1——单用户,就是咱们以前修改root帐户密码的模式, 2——多用户模式,但比3模式少了一个nfs服务 3——多用户命令行模式,最经常使用 4——保留级别暂时没用, 5——图形模式, 6——重启 80表示启动优先级 90表示关闭优先级 #description:xxxxxxxxx 脚本的描述信息
systemctl方法的参数设置(以nginx为例)
[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]服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为中止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、中止命令所有要求使用绝对路径。[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3。
https://www.liangzl.com/get-article-detail-15933.html
https://baijiahao.baidu.com/s?id=1593743216505421871&wfr=spider&for=pc