Linux系统启动后执行脚本

on February 10th, 2023 by 小桥 | No Comments »

在Linux系统启动之后执行脚本,传统的习惯是把脚本加到/etc/rc.local。家里R2S的rc.local加入了一些脚本,但是执行失败。初步怀疑是一个脚本需要联网才能执行成功,如果光猫跟R2S在同一时间重启,可能系统启动后网络还没恢复,会导致脚本执行失败。于是期望脚本在系统启动后一段时间执行,在rc.local中加入sleep可以实现这个目标,但是我不想让rc.local执行时间太长。搜索了一下,发现cron也是支持在系统启动后运行一次脚本的,只是以前从来没用过这个功能。

Instead of the first five fields, one of eight special strings may appear:
    string         meaning
    ------         -------
    @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 * * * *".

于是添加以下cron任务,系统启动后sleep 3分钟再执行指定脚本,期望能够解决问题:)

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# min hour  day  month weekday command
@reboot                        sleep 3m && /path/to/script

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA * Time limit is exhausted. Please reload the CAPTCHA.