Ubuntu 开机自动运行命令

Ubuntu开机自动运行自定义的命令,可以采用两种方式:

第一种shell脚本方式

1、进入 /etc/init.d/ 目录

[root@Ubuntu ~]# cd /etc/init.d/

2、新建一个自定义名称的sh脚本,这里以 xxx 名称为例建立一个 xxx.sh 的脚本

[root@Ubuntu /etc/init.d/]# vi XXX.sh        # xxx为你的脚本文件名

XXX.sh 脚本内容,编辑好以后保存退出 :wq

### BEGIN INIT INFO
# Provides:          svnd.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



bt restart        # 这里为需要执行的命令

注:脚本内容必须包含 ### BEGIN INIT INFO … ### END INIT INFO

不然会报错误:*missing LSB tags and overrides*

3、赋予脚本权限

[root@Ubuntu /etc/init.d/]# sudo chmod 775 ./XXX.sh        # xxx为你的脚本文件名 

4、然后执行以下命令

[root@Ubuntu /etc/init.d/]# sudo update-rc.d XXX.sh defaults 90        # xxx为你的脚本文件名
 System start/stop links for /etc/init.d/bt.sh already exist.

5、最后重启系统即可:reboot

附:取消开机启动脚本

[root@Ubuntu ~]# cd /etc/init.d                            # 进入/etc/init.d目录

[root@Ubuntu /etc/init.d/]# sudo update-rc.d -f XXX.sh remove         # 移除脚本,XXX为你的脚本文件名

第二种添加系统启动脚本方式:

1、编辑/etc/下的rc.local脚本

vi /etc/rc.local

2、然后把对应的需要执行的脚本写在 exit 0 前面,如下图所示

img

编辑好以后保存退出即可 : wq

转载:Ubuntu 开机自动运行命令

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值