jybox

不知道大家知不知道 crontab 有 @reboot 这样的用法

  •  
  •   jybox · Oct 29, 2013 · 10466 views
    This topic created in 4594 days ago, the information mentioned may be changed or developed.
    分享一下我前一阵的发现,感觉知道的人不多,在 V2EX 搜了一下也没人发过。

    共用服务器的时候,非 root 用户没法改 rc.locale, 又不能写在 .bashrc 里面,这时候就可以用 @reboot 把需要开机启动的程序写在 crontab 里。

    通常的 crontab 语法:

    0 * * * * /path/to/command

    crontab 还支持这样的语法:

    @reboot /path/to/command
    5 replies    1970-01-01 08:00:00 +08:00
    zorceta
        1
    zorceta  
       Oct 29, 2013 via Android
    那@start和@shutdown这种呢?
    eightii
        2
    eightii  
       Oct 29, 2013
    最好提供一下环境(系统、版本)。
    另外,@reboot之后的时间段怎么表示,直接@reboot * * * * * /path/to/command ?
    dorentus
        3
    dorentus  
       Oct 29, 2013
    @eightii

    @reboot 在这里是算作一个特殊的时间,后面就只能接命令,不能再设置别的时间了。

    man 5 crontab 可以看到还有其它的,比如我这边的 linux 里面显示有以下八个:

    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 * * * *".
    dorentus
        4
    dorentus  
       Oct 29, 2013
    @reboot 这种,不需要这个用户登录,系统启动后就会以这个用户的身份运行,很方便
    我的翻墙脚本都是这样写在 crontab 里面的……

    shadowsocks 客户端
    @reboot screen -dm /usr/local/bin/ss-local -v -c /usr/local/etc/ss-config.json

    shadowsocks 服务端
    @reboot screen -dm /usr/local/bin/ss-server -v -c ~/ss/config.json
    xiaket
        5
    xiaket  
       Oct 29, 2013
    记得测试过, @reboot在有些环境下是对重启cron服务就会跑, 有些环境下是只有重启机器才会跑.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3770 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 05:04 · PVG 13:04 · LAX 22:04 · JFK 01:04
    ♥ Do have faith in what you're doing.