Linux 开机自动启动脚本

1)编写要执行脚本的sh文件mysetup.sh

#!/bin/sh
### BEGIN INIT INFO
# Provides:          land.sh
# Required-start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the svnd.sh daemon
# Description:       starts svnd.sh using start-stop-daemon
### END INIT INFO

#任务脚本
#进入要执行脚本目录
cd /home/cbuav/working/opencv/target_land
#取得root权限,'123456'为密码,不用加引号,'ls'无实际作用
echo 123456 | sudo -S ls
#执行脚本./bin/mywork,sudo -S需要加上
sudo -S ./bin/mywork
#任务脚本

注释部分是必须内容,ubuntu 16.04中一定要加上该LSB信息,不然放入启动脚本的时候会报错无法开机启动。
任务脚本中,一般需要用到root权限,取得root权限和实际任务语句最好分开写,有些情况下写在一起不会成功。

2)将该sh文件移动到/etc/init.d/目录下,并修改权限

cp mysetup.sh /etc/init.d
sudo chmod 755 /etc/init.d/mysetup.sh
 
cd /etc/init.d
sudo update-rc.d mysetup.sh defaults 95

其中数字95是脚本启动的顺序号,按照自己的需要相应修改即可。在你有多个启动脚本,而它们之间又有先后启动的依赖关系时你就知道这个数字的具体作用了。

3)卸载启动脚本

 cd /etc/init.d
sudo update-rc.d -f mysetup.sh remove

猜你喜欢

转载自www.cnblogs.com/qccz123456/p/11688880.html