[ubuntu][deepin]系统增加自定义开机启动项
进行配置
- cd /etc/init.d/
- ls
- vim myScript
nginx实例
#! /bin/sh
# chkconfig:
# 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:
# Default-Stop:
# 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
fi $NGINX_BIN -c $CONFIG if [ "$?" != ]; then
echo " failed"
exit
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
fi $NGINX_BIN -s stop if [ "$?" != ] ; then
echo " failed. Use force-quit"
$ 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
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
fi kill $PID if [ "$?" != ]; then
echo " failed"
exit
else
echo " done"
fi
;; restart)
$ stop
sleep
$ 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
fi
;; configtest)
echo -n "Test nginx configure files... " $NGINX_BIN -t
;; *)
echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
exit
;; esac
模板
下面开始进行我们自己的配置
#! /bin/sh
# chkconfig:
# 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:
# Default-Stop:
# Short-Description: [启动项的简短说明]
# Description: [启动项的完整说明]
### END INIT INFO [需要执行的命令] exit
咱们自己的
#! /bin/sh
# chkconfig:
# 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:
# Default-Stop:
# Short-Description: myScript
# Description: myScript
### END INIT INFO echo "myScript,hello word~~~" >>/home/myScript.txt exit
~~~~~~~~~~~~~~
如果要运行脚本或者程序,可以定义变量,但是会涉及一个开机自启程序读取本地配置或者文件的问题。
不定义也行,哈哈哈
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/的配置加入这些级别中。