linux 开机自启动的两种方式

方法 1 – 使用 rc.local

sudo vi /etc/rc.local

在文件最后加上:

sh /root/script.sh &

如果是 CentOS,我们修改的是文件 /etc/rc.d/rc.local 而不是 /etc/rc.local。 

注意: 启动时执行的脚本,请一定保证是以 exit 0 结尾的。

 

方法 2 – 使用 Crontab

创建一个 cron 任务,这个任务在系统启动后等待 90 秒,然后执行命令和脚本。

要创建 cron 任务,打开终端并执行

$ crontab -e

然后输入下行内容,

@reboot ( sleep 90 ; sh /location/script.sh )

这里 /location/script.sh 就是待执行脚本的地址。

相关知识:

@reboot  Run once, at startup.
@yearly  Run once a year, “0 0 1 1 *”.
@annually   (same as @yearly)
@monthly   Run once a month, “0 0 1 * *”.
@weekly   Run once a week, “0 0 * * 0”.
@daily   Run once a day, “0 0 * * *”.
@midnight   (same as @daily)
@hourly   Run once an hour, “0 * * * *”.

相关文章:https://www.cnblogs.com/mianbaoshu/p/14086408.html

参考:

https://www.linuxprobe.com/automatic-running-methods.html

https://www.cnblogs.com/mianbaoshu/p/14086408.html

 

posted @ 2021-03-04 17:00  声声慢43  阅读(551)  评论(0编辑  收藏  举报