[ubuntu][deepin]系统增加自定义开机启动项

进行配置

cd /etc/init.d/

ls

vim myScript

nginx实例

#! /bin/sh

# chkconfig:2345 55 25# Description: Startup scriptfor 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 nginxusing 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" instart)

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."exit1fi

$NGINX_BIN-c $CONFIGif [ "$?" != 0]; then

echo"failed"exit1

elseecho"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."exit1fi

$NGINX_BIN-s stopif [ "$?" != 0] ; then

echo"failed. Use force-quit"$0 force-quitelseecho"done"fi

;;

status)

PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')if [ "$PID" != ""]; then

echo"nginx (pid $PID) is running..."

elseecho"nginx is stopped."exit0fi

;;

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."exit1fi

kill $PIDif [ "$?" != 0]; then

echo"failed"exit1

elseecho"done"fi

;;

restart)

$0stop

sleep1$0start

;;

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"

elseecho"nginx is not running, can't reload."exit1fi

;;

configtest)

echo-n "Test nginx configure files..."$NGINX_BIN-t

;;*)

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

esac

模板

下面开始进行我们自己的配置

#! /bin/sh

# chkconfig:2345 55 25# Description: Startup scriptfor 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

[需要执行的命令]

exit0

咱们自己的

#! /bin/sh

# chkconfig:2345 55 25# Description: Startup scriptfor 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

exit0

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

如果要运行脚本或者程序,可以定义变量,但是会涉及一个开机自启程序读取本地配置或者文件的问题。

不定义也行,哈哈哈

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

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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐