Skip to content

Instantly share code, notes, and snippets.

@huwan
Created June 22, 2014 10:49
Show Gist options
  • Save huwan/a601fffc0b7b930205ba to your computer and use it in GitHub Desktop.
Save huwan/a601fffc0b7b930205ba to your computer and use it in GitHub Desktop.
ubuntu 系统在开机启动后再自动重启一次
#!/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.
if [ ! -f /root/.reboot ]; then
echo " Times=0 , start at `date`" > /root/.reboot
fi
n=`cat /root/.reboot|awk '{print substr($1,7)}'`
if [ $n = "0" ]; then
echo " Times=1 , start at `date`" > /root/.reboot
#sleep 20s
reboot
else
rm /root/.reboot
fi
exit 0
@huwan
Copy link
Author

huwan commented Jun 22, 2014

工作中遇到有些网络配置需要在ubuntu 系统在开机启动后再重启一次才能生效的情况,修改/etc/rc.local 脚本实现自动重启一次。

主要是脚本会在启动后执行,通过文件的方式设置了一个可检测的标记,保证可以重启[一次],但不会一直重启。

脚本还可以简化为启动后检测某个文件是否存在,不存在就创建,然后重启,然后重启后发现文件存在,就删除即可。中间打印时间做个样子而已。

可能有用的参考资料:
http://blog.csdn.net/wzy_1988/article/details/8476901 Update-rc.d & rc.local管理Ubuntu的开机启动
http://blog.csdn.net/liguangyi/article/details/3771746 ubunut的rc.local不执行的问题
http://blog.163.com/lgh_2002/blog/static/44017526201032803748503/ ubuntu rc.local 为何不执行?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment