bash 如何在 Mac 操作系统上使用 Cron 更新自制软件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20528282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:57:53  来源:igfitidea点击:

how to update homebrew with Cron on Mac os

linuxmacosbashcroncrontab

提问by Ian Zhao

I've been discvering some long lasting linux techs to help automate my daily work. I found cronto be very powerful if I can use it to check the updates of some packages I have on my system.

我一直在发现一些持久的 linux 技术来帮助自动化我的日常工作。我发现cron如果我可以用它来检查我系统上的某些软件包的更新,它会非常强大。

For example, I want to update my Homebreweveryday at 11pm. What I did is, with sudo crontab -u user -e, I opened up crontabin Vim. And I put following commands into it, to make updates for homebrewand send me an email.

例如,我想在Homebrew每天晚上 11 点更新。我所做的是,用sudo crontab -u user -e,我crontab在 Vim 中打开。我将以下命令放入其中,以进行更新homebrew并向我发送电子邮件。

Here's the code:

这是代码:

[email protected]
* 23 * * * brew update

and I save it to wait for magic happens. Instead of excuting this command, in the email I recieved, it says /bin/sh: brew : command not found

我保存它以等待魔法发生。在我收到的电子邮件中,它没有执行此命令,而是说/bin/sh: brew : command not found

But when I type /bin/shin terminal to open shand type in brew updateit will update the Homebrew

但是当我输入/bin/sh终端打开sh并输入时,brew update它会更新Homebrew

What did I do wrong with my crontabconfiguration?

我的crontab配置做错了什么?

Any help will be appreciated!

任何帮助将不胜感激!

采纳答案by SimonSimCity

A cronjob is a good option, but I didn't want it to happen automatically. I found a scriptthat will notify you if a new version of a formula installed on your Mac is available.

cronjob 是一个不错的选择,但我不希望它自动发生。我找到了一个脚本,如果您的 Mac 上安装了新版本的公式,它会通知您。

I extended the scriptto not show pinned formulae in the notifier.

我扩展了脚本以不在通知程序中显示固定公式。

I decided to use a launchd-agent for the cronjb, because this also runs if Mac is started later. Cron-jobs just run if your mac is already on at that time.

我决定为 cronjb 使用 launchd-agent,因为如果稍后启动 Mac,它也会运行。如果当时您的 Mac 已经打开,Cron-jobs 就会运行。

For a comparison of cronjob vs launchd, I recommend reading this.

对于 cronjob 与 launchd 的比较,我建议阅读

Here's my configuration which runs every day at 10am and 3pm. The script, called by the agent, is located at /usr/local/bin/homebrew-update-notifier.

这是我每天上午 10 点和下午 3 点运行的配置。由代理调用的脚本位于/usr/local/bin/homebrew-update-notifier

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>EnableGlobbing</key>
    <false/>
    <key>Label</key>
    <string>homebrew.simonsimcity.update-notifier</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/usr/local/bin/homebrew-update-notifier</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/homebrew.simonsimcity.update-notifier.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/homebrew.simonsimcity.update-notifier.out</string>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>10</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>15</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
    </array>
</dict>
</plist>

You will now be notified if a new update is available. Call brew upgradeif you feel outdated, or include it in the script.

如果有新的更新可用,您现在会收到通知。brew upgrade如果您觉得过时,请致电,或将其包含在脚本中。

回答by Donovan

Cron doesn't have your PATH defined, make sure you always call commands with the full path, which is probably /usr/local/bin/brew update

Cron 没有定义你的 PATH,确保你总是使用完整路径调用命令,这可能是 /usr/local/bin/brew update

This is considered good practice to keep unwanted/unexpected commands from running. If someone put a malicious script called 'brew' somewhere else in your path, but before /usr/local/bin, it would get called instead.

这被认为是防止不需要/意​​外命令运行的好习惯。如果有人将名为 'brew' 的恶意脚本放在您路径中的其他位置,但在 /usr/local/bin 之前,它会被调用。

回答by Owen

Personally the way I handle this is:

我个人的处理方式是:

  • Download CronniX (it's discontinued but still works, e.g. brew cask install cronnix)

  • Setup the job within the UI via /usr/local/bin/brew updateto run every hour.

  • 下载 CronniX(它已停产但仍然有效,例如brew cask install cronnix

  • 在 UI 中设置作业通过/usr/local/bin/brew update每小时运行一次。

Let it tick over in the background!

让它在后台打勾!

enter image description here

在此处输入图片说明

回答by Eric

There's probably something wrong with your bashconfiguration. Make sure you set the PATHso that it contains the directory that brewis in. You may have it set in your ~/.profilebut make sure it's also set in your ~/.bashrc. Alternatively, you could set it in your crontabin the same way you set MAILTO

你的bash配置可能有问题。确保您设置了 ,PATH以便它包含所在的目录brew。您可以在您的目录中设置它,~/.profile但请确保它也在您的~/.bashrc. 或者,您可以crontab按照与设置相同的方式将其设置在MAILTO