centos7关于服务的开机自启动

1、systemctl的使用

查看全部服务命令:
systemctl list-unit-files --type service
查看服务
systemctl status name.service
启动服务
systemctl start name.service
停止服务
systemctl stop name.service
重启服务
systemctl restart name.service增加开机启动
systemctl enable name.service
删除开机启动
systemctl disable name.service
其中.service 可以省略。

2、开机服务配置

比如:tomcat服务开机自启动的配置:
vim /usr/lib/systemd/system/tomcat.service

[Unit]
Description=tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking  # 后台运行的形式
Environment="JAVA_HOME=/usr/local/jdk1.8"  # 这里为Java的环境变量,会自动补全后面的/bin/java;不会使用系统的环境变量
Environment=CATALINA_PID=/apps/tomcat/tomcat.pid # 这里只能是CATALINA_PID,不能命名为其他的
#延迟30秒启动
ExecStartPre=/bin/sleep 2
ExecStart=/apps/tomcat/bin/startup.sh # 服务的具体运行命令
ExecStop=/apps/tomcat/bin/shutdown.sh # 停止命令
ExecReload=/bin/kill -s HUP $MAINPID # 重启命令
RemainAfterExit=yes

# PrivateTmp=True表示给服务分配独立的临时空间
# 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]
WantedBy=multi-user.target # 运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

然后执行下面操作:
ln -s /etc/systemd/system/multi-user.target.wants/tomcat.service /usr/lib/systemd/system/tomcat.service

注意,上面配置完成之后一定要重新刷新一下配置:
systemctl daemon-reload
systemctl enable tomcat.service

systemctl start tomcat.service
systemctl status tomcat.service

3、redis的开机自启动设置:
vim /usr/lib/systemd/system/redis.service

[Unit]
Description=redis
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking  # 后台运行的形式
ExecStart=/etc/init.d/redis start # 服务的具体运行命令
ExecStop=/etc/init.d/redis stop # 停止命令

# PrivateTmp=True表示给服务分配独立的临时空间
# 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]
WantedBy=multi-user.target # 运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3


systemctl daemon-reload # 一定要重新加载

systemctl enable redis
systemctl start redis.service
systemctl status redis.service


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值