ubuntu开机自启动脚本

ubuntu开机自启动脚本

方法1. 编辑/etc/rc.local

rc.local脚本是一个ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令。该脚本位于/etc/路径下,需要root权限才能修改。该脚本的内容如下:

#!/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.
# 此处可添加任何sh命令,或执行某个sh脚本的命令,如:
echo $LD_LIBRARY
export PATH=/home/ubuntu/bin:$PATH
cd /home/ubuntu/tibet
sh OCR_Server.sh localhost 5081

#log 可以先增加日志输出功能,来查看某个开机自启动脚本最终未启动的原因,之后关于命令行的输出就默认保存在/tmp/rc.local.log文件里
exec 2> /tmp/rc.local.log  # send stderr from rc.local to a log file  
exec 1>&2                  # send stdout to the same log file  
set -x                     # tell sh to display commands before execution
sh test.sh 

exit 0

注意:

  • 一定要将命令添加在exit 0之前。里面可以直接写命令或者执行Shell脚本文件sh;
    如果是执行sh文件,那么要赋予执行权限sudo chmod +x xxx.sh

方法2. 添加开机脚本

/etc/init.d/目录下存放了所有需要开机自启的脚本,如ssh等自启动脚本。通过update-rc.d来管理(增加,删除等)开机启动服务。系统开机启动时会根据update-rc.d设置时的优先级依次运行各脚本启动相应的服务。详细命令如下:
cd /etc/init.d
#新建一个sslocal自启动脚本,内容如下:

sudo vim ss_local.sh
#! /bin/sh
# command content
# 脚本功能是开机自启动shadowsocks 翻墙服务
sslocal -c /home/ubuntu/shadowsocks.json

exit 0

#添加自启动脚本。在这里90表明一个优先级,越高表示执行的越晚
sudo update-rc.d ss_local.sh defaults 90  

# 从开机自启动项里删除自启动项
sudo update-rc.d -f ss_local.sh remove

重点:
ubuntu16下通过修改rc.local方式添加的开机自启动功能,无法使用tty1模式。即无法通过ctrl+alt+F1等进入tty1终端模式。而方法2则可以。尤其是当需要重装显卡驱动时,进不去tty1模式则很难办

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值