Ubuntu 设置开机启动命令/脚本 |
您所在的位置:网站首页 › ubuntu启动脚本命令 › Ubuntu 设置开机启动命令/脚本 |
Ubuntu 设置开机启动命令/脚本 Ubuntu 设置rc.local开机启动命令/脚本一般有两种方法: 方法一:rc.local脚本 rc.local脚本是一个Ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令。该脚本位于/etc/路径下,需要root权限才能修改。 注意:rc.local脚本里面启动的用户默认为root权限。 命令或脚本需要添加在exit 0之前 例如在rc.local中添加mkdir /root/test1234 表示在开机启动后自动在/root/下创建一个test1234目录 #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. /usr/local/bin/gen-friendlyelec-release . /etc/friendlyelec-releaseif [ ! -f /etc/firstuse ];then /bin/echo ${BOARD} > /etc/hostname /bin/sed -i "s/\(127.0.1.1\s*\).*/\1${BOARD}/g" /etc/hosts /bin/hostname ${BOARD} /bin/echo "0" > /etc/firstuse fi . /usr/bin/setqt4env /usr/bin/lcd2usb_print "CPU: {{CPU}}" "Mem: {{MEM}}" "IP: {{IP}}" "LoadAvg: {{L$ #/opt/QtE-Demo/run.sh& mkdir /root/test1234 exit 0 方法二:update-rc.d增加开机启动服务 在Ubuntu系统中添加一个开机启动脚本,操作如下: 1、新建脚本文件start_test.sh 代码如下: #!/bin/sh ### BEGIN INIT INFO # Provides: start_test.sh # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the start_test.sh # Description: starts start_test.sh # for security. ### END INIT INFO mkdir /root/start_test456 INIT INFO描述了该脚本为哪个应用程序提供服务。还说明了运行该应用程序之前需要先运行哪些应用程序,例如这里写的$all,表示该程序将在其他依赖程序都已经启动的情况下才启动。init脚本中最重要的描述可能就是Default-Start和Default-Stop,这里描述了该脚本在Linux系统中的运行级别,运行级别的定义如下: * level 0:停机 * level 1 :单用户 l * evel 2&3 :多用户 * level 4 : 用户自定义 * level 5:系统一般运行状态 * level 6:重启 Short-Description以及Description填写服务描述 2、设置权限 sudo chmod +x start_test.sh 3、拷贝到/etc/init.d/下 sudo cp ./start_test.sh /etc/init.d/ 4、将脚本添加到启动脚本 执行如下指令,在这里90表明一个优先级,越高表示执行的越晚 cd /etc/init.d/ sudo update-rc.d start_test.sh defaults 90 5、移除Ubuntu开机脚本 cd /etc/init.d sudo update-rc.d -f start_test.sh remove 6、通过sysv-rc-conf来管理上面启动服务的启动级别等 sudo sysv-rc-conf 7、update-rc.d的详细参数 使用update-rc.d命令需要指定脚本名称和一些参数,它的格式是这样的(需要在 root 权限下): update-rc.d [-n] [-f] remove update-rc.d [-n] defaults update-rc.d [-n] disable|enable [S|2|3|4|5] update-rc.d start|stop -n: not really -f: force 其中: • disable|enable:代表脚本还在/etc/init.d中,并设置当前状态是手动启动还是自动启动。 •start |stop:代表脚本还在/etc/init.d中,开机,并设置当前状态是开始运行还是停止运行。(启用后可配置开始运行与否) • NN:是一个决定启动顺序的两位数字值。(例如90大于80,因此80对应的脚本先启动或先停止) • runlevels:则指定了运行级别。 实例: (1)、添加一个新的启动脚本sample_init_script,并且指定为默认启动顺序、默认运行级别(注意:首先要有实际的文件存在于/etc/init.d,即若文件/etc/init.d/sample_init_script不存在,则该命令不会执行): update-rc.d sample_init_script defaults 上一条命令等效于(中间是一个英文句点符号): update-rc.d sample_init_script start 20 2 3 4 5 . stop 20 0 1 6 (2)、安装一个启动脚本sample_init_script,指定默认运行级别,但启动顺序为50: update-rc.d sample_init_script defaults 50 (3)、安装两个启动脚本A、B,让A先于B启动,后于B停止: update-rc.d A 10 40 update-rc.d B 20 30 (4)、删除一个启动脚本sample_init_script,如果脚本不存在则直接跳过: update-rc.d -f sample_init_script remove 这一条命令实际上做的就是一一删除所有位于/etc/rcX.d目录下指向/etc/init.d中sample_init_script的链接(可能存在多个链接文件),update-rc.d只不过简化了这一步骤。 (5)禁止Apache/MySQL相关组件开机自启: update-rc.d -f apache2 remove update-rc.d -f mysql remove 8、服务的启动停止状态 通过service,比如 sudo service xxx status sudo service xxx start sudo service xxx stop sudo service xxx restart 9、查看全部服务列表 sudo service --status-all |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |