Linux 配置开机自启动

博主使用的是Ubuntu 16.04,其它版本可能有偏差,但原理类似

方式一:rc.local

ubuntu在开机之后会执行/etc/rc.local文件中的脚本

xxx@xxx:~$ cat /etc/rc.local
#!/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.
bash ./usr/deploy.sh &
exit 0

在shell执行命令之后加 & 是为了让应用程序在后台运行,rc.local 也是一个脚本,主进程在运行这个脚本的时候必须能够返回,如果在这个脚本里面执行了一些死循环或其他无法返回的任务,整个系统就很可能卡死在这里,无法启动,所以,在这里运行的用户程序必须是能够返回或者本身就使用一些后台运行的进程。

指定用户运行

xxx@xxx:~$ cat /etc/rc.local
#!/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.
su - username -c "your command"
exit 0

方式二:/etc/init.d

第二种方式就是添加用户进程,将自己的脚本添加到/etc/init.d 并链接到自启动程序中。
将要运行的脚本使用以下指令:

chmod +x deploy.sh
sudo cp deploy.sh /etc/init.d/

将文件放到/etc/init.d目录中,然后将deploy.sh 脚本链接到开机运行序列中:

cd /etc/init.d
sudo update-rc.d deploy.sh defaults 98 
# update-rc.d是一个系统的链接工具
# defaults 98是指定了脚本的开机顺序,数字为0-99,数字越大则执行优先级越低
# 用户添加的程序最好选择低优先级的执行顺序,因为很可能我们的用户程序会依赖一些系统的应用进程

这样重新启动时,就可以看到deploy.sh正在运行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值