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


当前回答

已接受的答案确实提供了一种使用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将使用的环境运行脚本的方法。正如其他人指出的那样,这并不是调试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作业,运行env并将stdout重定向到一个文件。 使用该文件和“env -”一起创建与cron作业相同的环境。

别忘了,因为cron的父进程是init,所以它运行的程序没有控制终端。你可以用这样的工具来模拟:

http://libslack.org/daemon/

六年后回答:环境不匹配问题是systemd“计时器”作为cron替代品解决的问题之一。无论您是从CLI还是通过cron运行systemd“service”,它都接收到完全相同的环境,从而避免了环境不匹配的问题。

导致cron任务在手动传递时失败的最常见问题是cron设置的限制性默认$PATH,在Ubuntu 16.04中是这样的:

"/usr/bin:/bin"

相比之下,systemd在Ubuntu 16.04上设置的默认$PATH是:

"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

因此,systemd计时器已经有更好的机会找到二进制文件,而不需要进一步的麻烦。

systemd计时器的缺点是,设置它们需要更多的时间。您首先创建一个“服务”文件来定义您想要运行的内容,然后创建一个“计时器”文件来定义运行它的时间表,最后“启用”计时器来激活它。

在我的例子中,cron正在使用sh执行我的脚本,它不能执行一些bash语法。 在我的脚本中,我添加了env变量SHELL:

#!/bin/bash
SHELL=/bin/bash