日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CentOS 7添加开机启动服务脚本

發(fā)布時(shí)間:2025/3/11 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CentOS 7添加开机启动服务脚本 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文路徑:https://blog.csdn.net/wang123459/article/details/79063703

---------------------------------------------------------

一、添加開機(jī)自啟服務(wù)

在CentOS?7中添加開機(jī)自啟服務(wù)非常方便,只需要兩條命令(以Jenkins為例):
systemctl enable jenkins.service #設(shè)置jenkins服務(wù)為自啟動(dòng)服務(wù)
sysstemctl start? jenkins.service #啟動(dòng)jenkins服務(wù)

二、添加開機(jī)自啟腳本

在centos7中增加腳本有兩種常用的方法,以腳本autostart.sh為例:
#!/bin/bash
#description:開機(jī)自啟腳本
/usr/local/tomcat/bin/startup.sh? #啟動(dòng)tomcat

方法一

1、賦予腳本可執(zhí)行權(quán)限(/opt/script/autostart.sh是你的腳本路徑)
chmod +x /opt/script/autostart.sh

2、打開/etc/rc.d/rc.local或/etc/rc.local文件,在末尾增加如下內(nèi)容
su - user -c '/opt/script/autostart.sh'

3、在centos7中,/etc/rc.d/rc.local的權(quán)限被降低了,所以需要執(zhí)行如下命令賦予其可執(zhí)行權(quán)限
chmod +x /etc/rc.d/rc.local

方法二

1、將腳本移動(dòng)到/etc/rc.d/init.d目錄下
mv? /opt/script/autostart.sh /etc/rc.d/init.d

2、增加腳本的可執(zhí)行權(quán)限
chmod +x? /etc/rc.d/init.d/autostart.sh

3、添加腳本到開機(jī)自動(dòng)啟動(dòng)項(xiàng)目中
cd /etc/rc.d/init.d
chkconfig --add autostart.sh
chkconfig autostart.sh on

二、自定義服務(wù)文件,添加到系統(tǒng)服務(wù),通過Systemctl管理

1.寫服務(wù)文件

[Unit]:服務(wù)的說明
Description:描述服務(wù)
After:描述服務(wù)類別

[Service]服務(wù)運(yùn)行參數(shù)的設(shè)置
Type=forking是后臺運(yùn)行的形式
ExecStart為服務(wù)的具體運(yùn)行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務(wù)分配獨(dú)立的臨時(shí)空間
注意:啟動(dòng)、重啟、停止命令全部要求使用絕對路徑
[Install]服務(wù)安裝的相關(guān)設(shè)置,可設(shè)置為多用戶

示例:nginx.service

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

redis.service

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

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=kill -INT `cat /tmp/redis.pid`
User=www
Group=www

[Install]
WantedBy=multi-user.target

2.保存目錄
以754的權(quán)限保存在目錄:
/usr/lib/systemd/system?
3.設(shè)置開機(jī)自啟動(dòng)
任意目錄下執(zhí)行
systemctl enable nginx.service?
4.其他命令
啟動(dòng)nginx服務(wù)
systemctl start nginx.service
設(shè)置開機(jī)自啟動(dòng)
systemctl enable nginx.service
停止開機(jī)自啟動(dòng)
systemctl disable nginx.service
查看服務(wù)當(dāng)前狀態(tài)
systemctl status nginx.service
重新啟動(dòng)服務(wù)
systemctl restart nginx.service
查看所有已啟動(dòng)的服務(wù)
systemctl list-units --type=service

總結(jié)

以上是生活随笔為你收集整理的CentOS 7添加开机启动服务脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。