V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jybox
V2EX  ›  分享发现

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

  •  
  •   jybox · 2013-10-29 00:10:48 +08:00 · 9698 次点击
    这是一个创建于 3825 天前的主题,其中的信息可能已经有所发展或是发生改变。
    分享一下我前一阵的发现,感觉知道的人不多,在 V2EX 搜了一下也没人发过。

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

    通常的 crontab 语法:

    0 * * * * /path/to/command

    crontab 还支持这样的语法:

    @reboot /path/to/command
    5 条回复    1970-01-01 08:00:00 +08:00
    zorceta
        1
    zorceta  
       2013-10-29 06:32:17 +08:00 via Android
    那@start和@shutdown这种呢?
    eightii
        2
    eightii  
       2013-10-29 09:33:30 +08:00
    最好提供一下环境(系统、版本)。
    另外,@reboot之后的时间段怎么表示,直接@reboot * * * * * /path/to/command ?
    dorentus
        3
    dorentus  
       2013-10-29 11:09:04 +08:00
    @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  
       2013-10-29 11:12:20 +08:00
    @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  
       2013-10-29 11:26:11 +08:00
    记得测试过, @reboot在有些环境下是对重启cron服务就会跑, 有些环境下是只有重启机器才会跑.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2766 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:19 · PVG 20:19 · LAX 05:19 · JFK 08:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.