如何像从 cron 调用一样运行命令

iro*_*and 7 cron

我编写了一个脚本并将其设置为 cron 作业。但是由于环境变量的不同,它无法正常工作。

在这种情况下,我会稍微改变一下crontab -e并设置一个 cron 作业时间最接近的分钟,然后等待下一分钟来显示结果。我觉得这是一种完全荒谬的方法,但我不知道更好的方法。

如果有一种方法可以运行脚本,就像在 cron 作业中调用它一样,我将使用它。

有谁知道怎么做?

Ouk*_*uki 5

以下是相反的方法:强制 cron 执行使用您的登录环境:

bash -lc "your_command"

来自 bash 手册:

-c string     If the -c option is present, then commands are read from string.
              If there are arguments after the string, they are assigned to the
               positional parameters, starting with $0.
-l            Make bash act as if it had been invoked as a login shell
               (see INVOCATION below).
Run Code Online (Sandbox Code Playgroud)

调用(有点精简):

当 bash 作为交互式登录 shell 或带有 --login 选项的非交互式 shell 被调用时,它首先从文件 /etc/profile 中读取并执行命令(如果该文件存在)。读取该文件后,它会按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,并从第一个存在且可读的文件中读取并执行命令。启动 shell 时可以使用 --noprofile 选项来禁止此行为。

想了解更多: