Cron 作业未写入日志文件

And*_*ndG 1 shell scripting bash cron io-redirection

我有一个 shell 脚本,在执行时将日期写入日志文件。当我手动运行脚本时,正确的输出将写入文件。但是,这需要自动化,当我作为 cron 作业运行时,没有任何内容写入文件,我很困惑为什么。

定时任务:

0 * * * * tomcat /usr/bin/sh /apps/rdsreplication/snap_replication.sh
Run Code Online (Sandbox Code Playgroud)

示例代码:

#/bin/bash/

echo ---------------------------------------- >> create_snap.txt
echo Start time:  >> create_snap.txt
date >> create_snap.txt
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激!

gle*_*man 5

shell脚本需要使用日志文件的完整路径:

#/bin/bash/
# assuming you want the txt file in the same directory as the bash script
logfile="$(dirname "$0")/create_snap.txt"
{
    echo ----------------------------------------
    echo Start time:
    date 
} >> "$logfile"
Run Code Online (Sandbox Code Playgroud)