1. 程式人生 > >centos7如何添加開機啟動服務/腳本

centos7如何添加開機啟動服務/腳本

啟動服務 可執行 pos 路徑 命令 sha csharp 自啟動 init.d

一、添加開機自啟服務

在centos7中添加開機自啟服務非常方便,只需要兩條命令(以Jenkins為例):


systemctlenablejenkins.service #設置jenkins服務為自啟動服務
sysstemctl start  jenkins.service #啟動jenkins服務

 

二、添加開機自啟腳本

在centos7中增加腳本有兩種常用的方法,以腳本autostart.sh為例:

#!/bin/bash
#description:開機自啟腳本
/usr/local/tomcat/bin/startup.sh #啟動tomcat

  

方法一

1、賦予腳本可執行權限(/opt/script/autostart.sh是你的腳本路徑)

chmod+x/opt/script/autostart.sh

  

2、打開/etc/rc.d/rc/local文件,在末尾增加如下內容

chmod+x/etc/rc.d/rc.local

  

3、在centos7中,/etc/rc.d/rc.local的權限被降低了,所以需要執行如下命令賦予其可執行權限

chmod+x/etc/rc.d/rc.local

方法二

1、將腳本移動到/etc/rc.d/init.d目錄下

mv /opt/script/autostart.sh/etc/rc.d/init.d

  

2、增加腳本的可執行權限

chmod+x /etc/rc.d/init.d/autostart.sh

  

3、添加腳本到開機自動啟動項目中

cd/etc/rc.d/init.d
chkconfig --add autostart.sh
chkconfig autostart.sh on

  

centos7如何添加開機啟動服務/腳本