[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/的配置加入这些级别中。

当然还有别的启动方式,先会这一种吧。

 

posted @ 2019-08-08 09:26  landv  阅读(3512)  评论(0编辑  收藏  举报