[ubuntu][deepin]系統增加自定義開機啟動項


[ubuntu][deepin]系統增加自定義開機啟動項

進行配置

  • cd /etc/init.d/
  • ls
  • vim myScript

nginx實例

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  https://lnmp.org

NGINX_BIN='/usr/local/nginx/sbin/nginx'
CONFIG='/usr/local/nginx/conf/nginx.conf'

case "$1" in
    start)
        echo -n "Starting nginx... "

        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            echo "nginx (pid $PID) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIG

        if [ "$?" != 0 ]; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping nginx... "

        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" = "" ]; then
            echo "nginx is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            $0 force-quit
        else
            echo " done"
        fi
        ;;

    status)
        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            echo "nginx (pid $PID) is running..."
        else
            echo "nginx is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "Terminating nginx... "

        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" = "" ]; then
            echo "nginx is is stopped."
            exit 1
        fi

        kill $PID

        if [ "$?" != 0 ]; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload nginx... "

        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "nginx is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test nginx configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;

esac

 

模板

下面開始進行我們自己的配置

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides: [程序名稱,唯一]
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: [啟動項的簡短說明]
# Description: [啟動項的完整說明]
### END INIT INFO

[需要執行的命令]

exit 0

咱們自己的

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides: myScript
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: myScript
# Description: myScript
### END INIT INFO

echo "myScript,hello word~~~" >>/home/myScript.txt

exit 0

~~~~~~~~~~~~~~

如果要運行腳本或者程序,可以定義變量,但是會涉及一個開機自啟程序讀取本地配置或者文件的問題。

不定義也行,哈哈哈

BIN='/home/landv/go/src/aa/aa'

$BIN

BIN是一個變量自己隨便搞

~~~~~~~~~~~~~~

接下來,我們給myScript加上可執行的屬性

  • chmod + x myScript

然后加入開機啟動項目中

  “大便系列”

  • update-rc.d myScript defaults

 

刪除開機啟動項目

  • update-rc.d -f myScript remove

注意

/etc/init 是系統啟動

/etc/init.d是應用啟動

/etc/rc*.d這個是啟動級別,具體級別含義請查詢Linux啟動級別

update-rc.d 這條命令就是把/etc/init.d/的配置加入這些級別中。

當然還有別的啟動方式,先會這一種吧。

 


免責聲明!

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



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