有的脚本需要开机的时候执行一次,比如seafile的启动脚本,使用一键安装脚本并没有添加启动项,重启以后,需要手动执行这个脚本。再或者其它自己写的小脚本,需要在开机执行某操作的,都可以使用这个方法。

chmod +x /etc/rc.local

给予rc.local执行权限

编辑这个rc.local文件

vi /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.

exit 0

要执行的命令需要放到这个exit 0前面。但是仅仅将命令放到此处,重启以后发现脚本并没有被执行,猜测应该是系统在启动其它服务,导致此脚本没有执行,需要在前方加入等待命令。

#!/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.

sleep 10
sh /root/seafile.sh

exit 0

加入了一条sleep的命令,此处表示,等待10s,再执行以后的脚本。重启以后发现脚本正确执行了。