生產環境中添加以非root用戶進行nginx、tomcat、nodejs等開機自啟動


一、添加開機自啟服務
在centos7中添加開機自啟服務非常方便,只需要兩條命令(以nginx為例):
# systemctl enable nginx.service #設置nginx服務為自啟動服務
# sysstemctl start  nginx.service #啟動nginx服務
 
二、添加開機自啟腳本
在centos7中增加腳本有兩種常用的方法,以腳本autostart.sh為例:
# cat autostart.sh
 
#!/bin/bash
# description: 開機自啟腳本
/opt/tomcat/bin/startup.sh  #啟動tomcat
 
方式一:
1、賦予腳本可執行權限(/opt/script/autostart.sh是你的腳本路徑)
# chmod a+x /opt/script/autostart.sh
 
2、打開/etc/rc.d/rc.local文件,在末尾增加如下內容
/opt/script/autostart.sh
 
3、在centos7中,/etc/rc.d/rc.local的權限被降低了,所以需要執行如下命令賦予其可執行權限
# chmod a+x /etc/rc.d/rc.local
 
方式二:
1、將腳本移動到/etc/rc.d/init.d目錄下
# cp  /opt/script/autostart.sh /etc/rc.d/init.d
 
2、增加腳本的可執行權限
# chmod a+x  /etc/rc.d/init.d/autostart.sh
 
3、添加腳本到開機自動啟動項目中
# cd /etc/rc.d/init.d
# chkconfig --add autostart.sh
# chkconfig autostart.sh on
 
 
 
直接使用# sudo -u app sh /opt/tomcat/bin/startup.sh   無法正常啟動tomcat,因為未正常讀取到java的環境變量,即便是在app用戶家目錄下.bash_profile定義java環境變量仍然是無法正常啟動。
解決辦法:
在startup.sh中增加java環境變量,則可解決問題。
 
如果不使用 sudo -u app bash /opt/tomcat/bin/startup.sh,使用 su - app -c "bash /opt/tomcat/bin/startup.sh" 可正常啟動。
 
坑:
centos7系統的/etc/rc.d/rc.local默認沒有執行權限,所以如果將開機自啟動的命令寫在該處,這需增加執行權限
# chmod a+x /etc/rc.d/rc.local
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM