校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Linux 添加開機啟動方法(服務/腳本)

Linux 添加開機啟動方法(服務/腳本)

熱門標簽:外呼系統虛擬號碼 接電話機器人罵人 長春電銷外呼系統代理商 百度地圖標注尺寸無法顯示 代理外呼系統創業 大連電銷外呼系統運營商 泰州智能外呼系統排名 400電話申請知乎 400電話干嘛怎么申請信用卡

系統啟動時需要加載的配置文件

/etc/profile、/root/.bash_profile
/etc/bashrc、/root/.bashrc
/etc/profile.d/*.sh、/etc/profile.d/lang.sh
/etc/sysconfig/i18n、/etc/rc.local(/etc/rc.d/rc.local)

一、修改開機啟動文件:/etc/rc.local(或者/etc/rc.d/rc.local)

# 1.編輯rc.local文件
[root@localhost ~]# vi /etc/rc.local

# 2.修改rc.local文件,在 exit 0 前面加入以下命令。保存并退出。
/etc/init.d/mysqld start                     # mysql開機啟動
/etc/init.d/nginx start                     # nginx開機啟動
supervisord -c /etc/supervisor/supervisord.conf         # supervisord開機啟動
/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null

# 3.最后修改rc.local文件的執行權限
[root@localhost ~]# chmod +x /etc/rc.local
[root@localhost ~]# chmod 755 /etc/rc.local

二、自己寫一個shell腳本

將寫好的腳本(.sh文件)放到目錄 /etc/profile.d/ 下,系統啟動后就會自動執行該目錄下的所有shell腳本。

三、通過chkconfig命令設置

# 1.將(腳本)啟動文件移動到 /etc/init.d/或者/etc/rc.d/init.d/目錄下。(前者是后者的軟連接)
mv /www/wwwroot/test.sh /etc/rc.d/init.d

# 2.啟動文件前面務必添加如下三行代碼,否側會提示chkconfig不支持。
#!/bin/sh             告訴系統使用的shell,所以的shell腳本都是這樣
#chkconfig: 35 20 80        分別代表運行級別,啟動優先權,關閉優先權,此行代碼必須
#description: http server     自己隨便發揮!!!,此行代碼必須
/bin/echo $(/bin/date +%F_%T) >> /tmp/test.log

# 3.增加腳本的可執行權限
chmod +x /etc/rc.d/init.d/test.sh

# 4.添加腳本到開機自動啟動項目中。添加到chkconfig,開機自啟動。
[root@localhost ~]# cd /etc/rc.d/init.d
[root@localhost ~]# chkconfig --add test.sh
[root@localhost ~]# chkconfig test.sh on

# 5.關閉開機啟動 
[root@localhost ~]# chkconfig test.sh off

# 6.從chkconfig管理中刪除test.sh
[root@localhost ~]# chkconfig --del test.sh

# 7.查看chkconfig管理
[root@localhost ~]# chkconfig --list test.sh

四、自定義服務文件,添加到系統服務,通過Systemctl管理

1.寫服務文件:如nginx.service、redis.service、supervisord.service

[Unit]:服務的說明
Description:描述服務
After:描述服務類別

[Service]服務運行參數的設置
Type=forking      是后臺運行的形式
ExecStart        為服務的具體運行命令
ExecReload       為服務的重啟命令
ExecStop        為服務的停止命令
PrivateTmp=True     表示給服務分配獨立的臨時空間
注意:啟動、重啟、停止命令全部要求使用絕對路徑

[Install]        服務安裝的相關設置,可設置為多用戶
WantedBy=multi-user.target 

2.文件保存在目錄下:以754的權限。目錄路徑:/usr/lib/systemd/system。如上面的supervisord.service文件放在這個目錄下面。

[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[root@localhost ~]# cat /usr/lib/systemd/system/supervisord.service

3.設置開機自啟動(任意目錄下執行)。如果執行啟動命令報錯,則執行:systemctl daemon-reload

設置開機自啟動
[root@localhost ~]# systemctl enable nginx.service    
[root@localhost ~]# systemctl enable supervisord

停止開機自啟動
[root@localhost ~]# systemctl disable nginx.service
[root@localhost ~]# systemctl disable supervisord

驗證一下是否為開機啟動
[root@localhost ~]# systemctl is-enabled nginx
[root@localhost ~]# systemctl is-enabled supervisord

4.其他命令

啟動nginx服務
[root@localhost ~]# systemctl start nginx.service

停止nginx服務
[root@localhost ~]# systemctl start nginx.service

重啟nginx服務
[root@localhost ~]# systemctl restart nginx.service

查看nginx服務當前狀態
[root@localhost ~]# systemctl status nginx.service

查看所有已啟動的服務
[root@localhost ~]# systemctl list-units --type=service

5.服務文件示例:

# supervisord.service進程管理服務文件
[Unit]
Description=Process Monitoring and Control Daemon  # 內容自己定義:Description=Supervisor daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop= /usr/bin/supervisorctl shutdown 
ExecReload=/usr/bin/supervisorctl reload
Restart=on-failure
RestartSec=42s
KillMode=process 

[Install]
WantedBy=multi-user.target

# 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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

標簽:安陽 興安盟 臺灣 大慶 雅安 中衛 長治 清遠

巨人網絡通訊聲明:本文標題《Linux 添加開機啟動方法(服務/腳本)》,本文關鍵詞  Linux,添加,開機,啟動,方法,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Linux 添加開機啟動方法(服務/腳本)》相關的同類信息!
  • 本頁收集關于Linux 添加開機啟動方法(服務/腳本)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 海南省| 波密县| 合作市| 桦南县| 延川县| 榆树市| 阿荣旗| 萨嘎县| 太谷县| 金秀| 额济纳旗| 大姚县| 霍林郭勒市| 桃源县| 琼海市| 双江| 历史| 景宁| 白沙| 年辖:市辖区| 西畴县| 蓝田县| 资阳市| 兴安盟| 宁明县| 和平县| 迁西县| 正镶白旗| 新龙县| 大庆市| 江安县| 普兰县| 长泰县| 分宜县| 汉中市| 遂平县| 聂拉木县| 连平县| 桦南县| 阿克陶县| 鄂温|