如何将 Linux 的多行 crontab 定时任务写在一个 macos 的 launchctl 控制的 plist 里面? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
mylovesaber
V2EX    macOS

如何将 Linux 的多行 crontab 定时任务写在一个 macos 的 launchctl 控制的 plist 里面?

  •  
  •   mylovesaber 2021-12-23 20:49:32 +08:00 2334 次点击
    这是一个创建于 1396 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我写了一个自动更新 github host 的脚本,它往 /etc/crontab 里面写了三个功能:

    */30 * * * * root /usr/bin/bash /usr/bin/hosts-tool run * * */3 * * root /usr/bin/bash /usr/bin/hosts-tool updatefrom gitee * * */10 * * root /usr/bin/bash /usr/bin/hosts-tool rmlog 

    我想兼容一下 macos ,网上查了下,crontab 在 macos 中被弃用了,一律是 launchd 控制,类似 systemd 这种东西,然后编写可用的 plist 的话,网上查了半天好像都只有单个例子,对于一个程序有多个功能选项且不同功能选项的运行定时不同的情况,网上好像没找到写法,如果按照单个例子写的话得写三个 plist ,有没有办法可以把三个功能写在一个 plist 中啊?新手刚接触 macos 还不太熟,先谢过各位了 脚本:

    https://github.com/mylovesaber/auto_update_github_hosts

    7 条回复    2021-12-24 01:37:34 +08:00
    EnochZack
        1
    EnochZack  
       2021-12-23 21:22:08 +08:00 via Android
    你可以写进一个脚本里,再写一个 plist 执行这个脚本
    minsheng
        2
    minsheng  
       2021-12-23 22:06:10 +08:00
    StartCalendarInterval <dictionary of integers or array of dictionary of
    integers>
    This optional key causes the job to be started every calendar interval as
    specified. Missing arguments are considered to be wildcard. The semantics
    are much like crontab(5). Unlike cron which skips job invocations when
    the computer is asleep, launchd will start the job the next time the com-
    puter wakes up. If multiple intervals transpire before the computer is
    woken, those events will be coalesced into one event upon wake from
    sleep.
    minsheng
        3
    minsheng  
       2021-12-23 22:06:24 +08:00
    RayGZJ
        4
    RayGZJ  
       2021-12-23 23:27:59 +08:00 via iPhone
    试试 Apple Script ?
    minamike
        5
    minamike  
       2021-12-24 01:09:51 +08:00 via iPhone
    mac 的 crontab 还是能用的啊
    mylovesaber
        6
    mylovesaber  
    OP
       2021-12-24 01:14:48 +08:00
    @minamike 是的,我因为信了网上说的苹果官方有意弃用 crontab 才折腾查找老半天,最终发现原来 launchd 就目前的功能性来看,是从根本上无法替代我这种用法下的 crontab ,为实现相同功能,launched 需要写三百行左右的规则才能替代 crontab ,但一旦需要改动时间的话,等于推倒重来,然后直接 `echo "*/30 * * * * root /usr/bin/bash /usr/bin/hosts-tool run" >> /var/at/tabs/root` 就可以了。。。。
    IgniteWhite
        7
    IgniteWhite  
       2021-12-24 01:37:34 +08:00
    你就原样写到 mac 的 crontab 就好。mac 的 cronjob 只是由 launchd 接管,但是还在。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     918 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 18:51 PVG 02:51 LAX 11:51 JFK 14:51
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86