【问题标题】:Bash script runs fine when called directly, but cron seemingly refuses to run it at all?直接调用 Bash 脚本运行良好,但 cron 似乎根本拒绝运行它?
【发布时间】:2021-07-17 01:50:02
【问题描述】:

我对此束手无策。我有以下脚本:

#!/usr/bin/env bash

declare -a nriArray=(newrelic-cli node-newrelic nri-flex helm-charts infrastructure-agent opentelemetry-exporter-java opentelemetry-exporter-go newrelic-lambda-cli newrelic-node-apollo-server-plugin nri-kubernetes nri-prometheus nri-redis infrastructure-bundle newrelic-lambda-layers nri-jmx newrelic-winston-logenricher-node nri-cassandra micrometer-registry-newrelic newrelic-fluent-bit-output nri-kafka node-newrelic-aws-sdk nri-elasticsearch nri-mysql nri-nagios nri-snmp node-newrelic-superagent nri-kube-events nri-mssql newrelic-module-util-java newrelic-logenricher-dotnet aws-log-ingestion newrelic-lambda-tracer-java nri-docker nri-oracledb k8s-metadata-injection nri-discovery-kubernetes nri-haproxy nri-postgresql node-native-metrics nri-consul nri-rabbitmq nri-vsphere nri-winservices nri-ecs newrelic-airflow-plugin newrelic-monolog-logenricher-php node-newrelic-koa nri-f5 aws_s3_log_ingestion_lambda dropwizard-metrics-newrelic k8s-webhook-cert-manager newrelic-fluentd-output nri-couchbase nri-memcached nri-mongodb java-aws-lambda logstash-output-plugin nri-apache nri-varnish java-log-extensions nri-nginx nri-statsd newrelic-opencensus-exporter-go newrelic-opencensus-exporter-python python-agent-extension)
cd /Users/aschneider/NewRelic-OpenSource/

counter=0
while [[ "$counter" -lt "${#nriArray[@]}" ]]; do

    cd "${nriArray[$counter]}"
    git pull &>> /Users/aschneider/NewRelic-OpenSource/cronLog.log
    gsed -i '/Already up to date./d' /Users/aschneider/NewRelic-OpenSource/cronLog.log
    git clean -q -d -f
    cd ..
    ((counter=counter+1))

done

printf "\n\n" >> /Users/aschneider/NewRelic-OpenSource/cronLog.log
holdingVar=$(cat -s /Users/aschneider/NewRelic-OpenSource/cronLog.log) && echo "$holdingVar" > /Users/aschneider/NewRelic-OpenSource/cronLog.log
/Applications/Utilities/terminal-notifier.app/Contents/MacOS/terminal-notifier -message "~/NewRelic-OpenSource/ repositories were updated."

脚本以递归方式遍历 GitHub 存储库数组,提取对它们的任何更新,然后移至下一个文件夹。它删除了所有说明文件夹是最新的行,以便仅保留日志文件中的更改,然后它使用终端通知程序在每次更新后提醒我。当我使用bash ~/NewRelic-OpenSource/updateOpenSource.sh 运行它时它可以完美运行,但是当我将脚本放入 crontab 时它甚至似乎都没有执行。我的crontab -l 输出如下:

SHELL=/usr/local/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
*/30 8-17 * * 1-5 /usr/local/bin/bash /Users/aschneider/NewRelic-OpenSource/updateOpenSources.sh

我在工作日的早上 8 点到下午 5 点每 30 分钟运行一次。即使我切换到* * * * * [command],它也不会做任何事情,所以我认为这不是调度语法问题。我目前在我的 MacBook 上运行它,它使用 GNU bash, version 5.1.4(1)-release (x86_64-apple-darwin20.2.0)。我尝试将 PATH 定义添加到 crontab 以及脚本本身。我还验证了该脚本可以使用chmod +x {script} 执行。关于我在这里做错了什么有什么想法吗?

【问题讨论】:

  • stackoverflow.com/tags/cron/info 可能会有所帮助。在您的 crontab 环境与终端会话中,您的 PATH 几乎总是不同的。祝你好运。
  • 作为一只古老的恐龙,我对“new relics”知之甚少,但也许你想用命令的输出填充你的数组...declare -a nriArray=($(newrelic-cli ...))
  • 还要向自己证明最基本的 cmd 在 cron 中工作,* * * * * /bin/date >> /tmp/date_test ?祝你好运。
  • @shellter 我也会去那边看看。我注意到其中大部分归结为 PATH 问题,但在尝试了大多数其他解决方案后,我仍然没有找到解决方法。感谢您的提示!
  • @shellter 我将* * * * * /bin/date >> /tmp/date_test 添加到 crontab 中,它确实输出正确,所以至少我知道 cron 服务正在运行并且可以正常工作。

标签: bash macos cron


【解决方案1】:

默认情况下,Cronjobs 以 root 身份运行。如果您手动运行脚本,您可能希望以通常运行脚本的用户身份运行。例如,

*/5 * * * * (add the username here) sample_executable

我也读过,但我可能错了他们没有使用你的 PATH 变量。所以在运行脚本之前,所有的可执行文件都应该被完全引用。

还要检查在 crontab 中运行脚本的权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多