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

2013-10-29 00:10:48 +08:00
 jybox
分享一下我前一阵的发现,感觉知道的人不多,在 V2EX 搜了一下也没人发过。

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

通常的 crontab 语法:

0 * * * * /path/to/command

crontab 还支持这样的语法:

@reboot /path/to/command
9707 次点击
所在节点    分享发现
5 条回复
zorceta
2013-10-29 06:32:17 +08:00
那@start和@shutdown这种呢?
eightii
2013-10-29 09:33:30 +08:00
最好提供一下环境(系统、版本)。
另外,@reboot之后的时间段怎么表示,直接@reboot * * * * * /path/to/command ?
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
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
2013-10-29 11:26:11 +08:00
记得测试过, @reboot在有些环境下是对重启cron服务就会跑, 有些环境下是只有重启机器才会跑.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/87227

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX