테스트/디버깅을 위해 cron이 지금 작업을 실행하도록 하려면 어떻게 해야 합니까? 일정 변경 없이!

테스트/디버깅을 위해 cron이 지금 작업을 실행하도록 하려면 어떻게 해야 합니까? 일정 변경 없이!

일정을 변경하는 것 외에 매일 실행되도록 예약된 cron 작업이 있습니다. 지금 바로 명령을 테스트 실행하여 의도한 대로 작동하는지 확인할 수 있는 다른 방법이 있습니까?

편집: (의견에서) 쉘(내 쉘)에 명령을 입력하면 명령이 제대로 작동한다는 것을 알고 있지만 cron이 실행할 때 올바르게 작동하는지 알고 싶습니다. ENV 또는 쉘 특정 항목(~ 확장)의 영향을 받을 수 있습니다. ) 또는 소유권 및 허가 항목 또는 ...

답변1

다음 명령을 사용하여 crontab을 강제로 실행할 수 있습니다.

run-parts /etc/cron.daily

답변2

다음에 설명된 대로 cron 사용자 환경을 시뮬레이션할 수 있습니다."크론 작업을 수동으로 즉시 실행". 이를 통해 cron 사용자로 실행될 때 작업이 작동하는지 테스트할 수 있습니다.


링크에서 발췌:


1 단계: 나는 사용자의 crontab에 다음 줄을 임시로 넣었습니다.

* * * * *   /usr/bin/env > /home/username/tmp/cron-env

그런 다음 파일이 작성되면 꺼냈습니다.

2 단계: 다음을 포함하는 작은 cron 실행 bash 스크립트를 만들었습니다.

#!/bin/bash
/usr/bin/env -i $(cat /home/username/tmp/cron-env) "$@"

그래서 문제의 사용자로서 저는 다음을 수행할 수 있었습니다.

    run-as-cron /the/problematic/script --with arguments --and parameters

답변3

내가 아는 한 cron에는 특정 시간에 스케줄 명령을 실행하는 특별한 목적이 있으므로 이를 직접 수행할 수 있는 방법은 없습니다. 따라서 가장 좋은 방법은 (임시) crontab 항목을 수동으로 생성하거나 환경을 제거하고 재설정하는 스크립트를 작성하는 것입니다.

"환경 제거 및 재설정"에 대한 설명:

환경을 제거하는 래퍼 스크립트를 시작하면 스크립트를 시작하기 전에 env -i저장된 환경을 소싱할 수 있습니다(모든 변수를 먼저 설정하여 내보내야 함 ).set -a

저장된 환경은 cron 작업의 기본 환경이 되며, cronjob으로 실행하여 env(또는 cron 작업이 사용하는 쉘에 따라) 출력을 저장하여 기록됩니다.declare -p

답변4

cron 작업을 디버그해야 하는 필요성을 느낀 후 다음 스크립트를 작성했습니다. 명령을 실행하기 전에 cron과 정확히 동일한 조건을 시뮬레이션하려고 노력합니다(수정된 환경을 포함하지만 비대화형 쉘, 연결된 터미널이 없는 등과도 관련이 있음).

명령/스크립트를 인수로 사용하여 호출하면 크론 작업을 디버그할 수 있는 즉각적이고 고통 없는 방법이 있습니다. 또한 GitHub에서도 호스팅되고 업데이트될 수도 있습니다.run-as-cron.sh:

#!/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 redirect the output into files
# and provide /dev/null as input
# (so that the command is executed without an open terminal
# on any standard file descriptor)
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"

관련 정보