我通常有几个问题,如何cron执行脚本,因为他们通常没有我的环境设置。是否有一种方法可以调用bash(?)以同样的方式cron,这样我就可以在安装它们之前测试脚本?


当前回答

取决于shell的账户

sudo su
env -i /bin/sh

or

sudo su
env -i /bin/bash --noprofile --norc

从http://matthew.mceachen.us/blog/howto -模拟- - cron -环境- 1018. - html

其他回答

取决于shell的账户

sudo su
env -i /bin/sh

or

sudo su
env -i /bin/bash --noprofile --norc

从http://matthew.mceachen.us/blog/howto -模拟- - cron -环境- 1018. - html

已接受的答案确实提供了一种使用cron将使用的环境运行脚本的方法。正如其他人指出的那样,这并不是调试cron作业所需的唯一标准。

事实上,cron也使用非交互式终端,没有附加的输入等等。

如果这有帮助的话,我已经编写了一个脚本,可以轻松地运行命令/脚本,就像由cron运行一样。使用命令/脚本作为第一个参数调用它,就没问题了。

这个脚本也托管(可能更新)在Github上。

#!/bin/bash
# Run as if it was called from cron, that is to say:
#  * with a modified environment
#  * with a specific shell, which may or may not be bash
#  * without an attached input terminal
#  * in a non-interactive shell

function usage(){
    echo "$0 - Run a script or a command as it would be in a cron job, then display its output"
    echo "Usage:"
    echo "   $0 [command | script]"
}

if [ "$1" == "-h" -o "$1" == "--help" ]; then
    usage
    exit 0
fi

if [ $(whoami) != "root" ]; then
    echo "Only root is supported at the moment"
    exit 1
fi

# This file should contain the cron environment.
cron_env="/root/cron-env"
if [ ! -f "$cron_env" ]; then
    echo "Unable to find $cron_env"
    echo "To generate it, run \"/usr/bin/env > /root/cron-env\" as a cron job"
    exit 0
fi

# It will be a nightmare to expand "$@" inside a shell -c argument.
# Let's rather generate a string where we manually expand-and-quote the arguments
env_string="/usr/bin/env -i "
for envi in $(cat "$cron_env"); do
   env_string="${env_string} $envi "
done

cmd_string=""
for arg in "$@"; do
    cmd_string="${cmd_string} \"${arg}\" "
done

# Which shell should we use?
the_shell=$(grep -E "^SHELL=" /root/cron-env | sed 's/SHELL=//')
echo "Running with $the_shell the following command: $cmd_string"


# Let's route the output in a file
# and do not provide any input (so that the command is executed without an attached terminal)
so=$(mktemp "/tmp/fakecron.out.XXXX")
se=$(mktemp "/tmp/fakecron.err.XXXX")
"$the_shell" -c "$env_string $cmd_string" >"$so" 2>"$se" < /dev/null

echo -e "Done. Here is \033[1mstdout\033[0m:"
cat "$so"
echo -e "Done. Here is \033[1mstderr\033[0m:"
cat "$se"
rm "$so" "$se"

默认情况下,cron使用您系统中的sh来执行它的作业。这可能是实际的Bourne shell或破折号,ash, ksh或bash(或另一个)符号链接到sh(并因此在POSIX模式下运行)。

最好的方法是确保您的脚本拥有它们需要的东西,并假设没有为它们提供任何东西。因此,您应该使用完整的目录规范并自己设置环境变量(如$PATH)。

我不相信有;我所知道的测试cron作业的唯一方法是设置它在未来运行一到两分钟,然后等待。

回答https://stackoverflow.com/a/2546509/5593430展示了如何获取cron环境并将其用于您的脚本。但是请注意,环境会因所使用的crontab文件而有所不同。我创建了三个不同的cron条目,通过env >日志保存环境。这些是在Amazon Linux 4.4.35-33.55.amzn1.x86_64上的结果。

1. 使用root用户创建全局/etc/crontab

MAILTO=root
SHELL=/bin/bash
USER=root
PATH=/sbin:/bin:/usr/sbin:/usr/bin
PWD=/
LANG=en_US.UTF-8
SHLVL=1
HOME=/
LOGNAME=root
_=/bin/env

2. root用户crontab (crontab -e)

SHELL=/bin/sh
USER=root
PATH=/usr/bin:/bin
PWD=/root
LANG=en_US.UTF-8
SHLVL=1
HOME=/root
LOGNAME=root
_=/usr/bin/env

3./etc/cron.hour /脚本

MAILTO=root
SHELL=/bin/bash
USER=root
PATH=/sbin:/bin:/usr/sbin:/usr/bin
_=/bin/env
PWD=/
LANG=en_US.UTF-8
SHLVL=3
HOME=/
LOGNAME=root

最重要的是PATH, PWD和HOME不同。确保在cron脚本中设置这些以依赖稳定的环境。