bash - 调试我的 cron 应用程序

标签 bash shell ubuntu amazon-s3 s3cmd

我有这样的代码:

set -e
set -x
folderName=$(echo `date +%Y/%m/%d/`);
fileName=x.x.x.x.x.x.x.log 
cp x.x.x.x.x.x/$1 $fileName  
gzip $fileName  
s3cmd put $fileName.gz s3://x.x.x.x.x/$folderName    
rm $fileName.gz

如果我像这样运行,这工作正常:

./shell logfilelocation

当我像这样添加到 crontab 时:

* * * * * /home/x.x.x/testing/s3 -f x.x.x.log >> /tmp/mys3Log

我等待着!文件 mys3Log 被创建。但里面没有任何内容!我期望命令执行的结果(因为我在代码中使用了 set -e ; set -x )应该进入 mys3Log 文件,因为我正在进行重定向那里。

但是出了点问题。我对 bash 编程和 cron 很陌生。

我哪里出错了?

提前致谢。

最佳答案

cron 没有与交互式 shell 相同的环境,因此在脚本的开头、shebang 之后添加:

source ~/.bashrc || source /etc/profile

并删除 set -e 以查看发生了什么。

在您的 crontab 中,要记录错误和输出(STDERRSTDOUT),您需要执行以下操作:

* * * * * /home/x.x.x/testing/s3 -f x.x.x.log >> /tmp/mys3Log 2>&1

此外,在脚本的第 4 行,您使用的是从未声明的变量 $name

最后但并非最不重要的一点是,就像 Janauary 所说的那样,在第一行添加 #!/bin/bash shegang 并确保您的脚本具有可执行权限。 :chmod +x script.sh

关于bash - 调试我的 cron 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12857907/

相关文章:

c - Ubuntu 11.10 上的 OpenCV && S2i Harpia

qt - 在 Ubuntu 12 上编译 Qt 4.8.3 失败

shell - shell中 'if [ -s filename]'是什么意思?

linux - 如何在 shell 中获取一年中的某一天?

python - 当 Ubuntu 21.10 xrdp 上的远程桌面时,tkinter 与 sudo 行为不端

bash - 刷新前 N 行并将光标重置到带有转义序列的当前行的末尾?

c# - 使用 shell 命令从 C# 程序中下载 MySQL 表是否合适?

bash - 使用特定窗口坐标从命令行启动 Google Chrome

bash - aws cli s3 同步 : how to exclude multiple files

bash - 在 bash 中,有没有办法在整数列表中搜索连续序列并删除这些序列中除最后一个数字之外的所有数字?