【问题标题】:Running acme cron job into .sh file due to 'characters not allowed' for cron job command由于 cron 作业命令的“字符不允许”,将 acme cron 作业运行到 .sh 文件中
【发布时间】:2021-04-29 17:23:30
【问题描述】:

我的托管服务提供商面板不允许添加下一个 cron 作业命令:

"/home/username/.acme.sh"/acme.sh --cron --home "/home/username/.acme.sh" > /dev/null

由于“某些字符不允许用于 cron 作业命令”错误。

在阅读了this post 的相关信息后,我试图了解如何从脚本 (~/run-acme.sh) 运行它并将其添加到 cron 作业中,但不知道该怎么做(该示例使用PHP 脚本)。

这是run-acme.sh 脚本的内容,但不知道在命令前写什么(source.bash),正如this answer 建议的那样:

选项 1

#!/usr/bin/bash
bash "/home/username/.acme.sh"/acme.sh --cron --home "/home/username/.acme.sh" > /dev/null

选项 2

#!/usr/bin/bash
source "/home/username/.acme.sh"/acme.sh --cron --home "/home/username/.acme.sh" > /dev/null

选项 3

#!/usr/bin/bash
. "/home/username/.acme.sh"/acme.sh --cron --home "/home/username/.acme.sh" > /dev/null

这些选项中的哪一个会起作用?它们的运行结果是否相同?

重要提示:无权访问 crontab/SSH

【问题讨论】:

  • 如果标题不是很清楚但我不知道如何命名(可能很长),请原谅
  • 如果你试图在另一个 shell/bash 中运行一个 shell/bash -- 只需使用 sh /var/www/yourfile.shbash /var/www/yourfile.sh -- 它会将脚本中的命令分叉到子进程中。如果我正确理解您的问题,就足够简单了..
  • 是什么让人们失望.. --cron --home 不是“标准”标志,所以你想要完成的可能需要更多解释/澄清。
  • @Zak 对 cron 一无所知,但 --cron --home 是来自 acme.sh script 的参数
  • 看起来重定向 > 是不允许的。因此,将该行放入脚本中并简单地调用它。

标签: bash cron acme.sh


【解决方案1】:

我相信您需要选项 1,因为您想运行 acme.sh 脚本。

选项 2 和选项 3 在 bash 中本质上是等价的,因为 source 是 bash 中 . 的别名。但是,它们在sh不等效,因为. 存在于shsource 不存在(这是因为source 是非POSIX bash 扩展)。

当使用source. 时,这类似于在Haskell 的GHCi 或Scala 的REPL 中使用:load,或者在Lisp 或Clojure 中使用loadsource. 命令告诉 shell 在当前 shell 的上下文中读取、解析和评估写在单独源文件中的代码。本质上,它允许在单独文件中定义的函数和变量在“调用”shell 脚本的上下文中可用。

选项 1 的作用是运行 acme.sh。您需要此选项是因为您不希望在当前 shell 中运行 acme.sh 脚本(您可能在当前 shell 中已经定义了一些别名或函数,这些别名或函数可能会导致命名冲突)。运行bash acme.sh 与运行python my_code.py 类似:bash 解释器将执行acme.sh 的内容。

有关source. 的更多信息,请参阅:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 2015-11-15
    • 2013-07-24
    • 2020-06-10
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多