我有一个简单的Bash脚本,可以从命令行正常运行。它需要在脚本开头附近写入两个临时文件。当我将其粘贴在cronjob中时,它将创建临时文件,但不会向其中写入任何数据。这些文件的大小为零。每次cron作业重新启动时,我都希望临时文件被覆盖。
cron条目基本上如下所示:
0 */4 * * * /opt/myscripts/boringScript.sh
在脚本中是:
...
echo "Starting boring inventory script" >> /opt/myscripts/boringScriptLog.out
countingScript.sh | grep $paramONE | sed s#\ ##g > tempResults1.txt
countingScript.sh | grep $paramTWO | sed s#\ ##g > tempResults2.txt
do
// more stuff with the above temp files
...
done
...
有什么想法为什么不将任何数据写入cron中的临时文件?
谢谢!
由于以下两个原因之一,它可能无法写入tempResults文件:
在处理cron时,为读取或写入的所有脚本和文件指定绝对路径是一个非常非常非常好的主意。我在您的脚本中看不到它。
例如,您正在指定的完整路径/opt/myscripts/boringScriptLog.out
,但是依赖于countingScript.sh,tempResults1.txt和tempResults2.txt在您当前的工作目录中,并且您的用户对此具有写访问权。
由于脚本正在运行,因此您至少具有对countingScript.sh所在目录的读取和执行权限,但没有写权限。
尝试使用
/path/to/my/countingScript.sh | grep $paramONE | sed s#\ ##g > /tmp/tempResults1.txt
/path/to/my/countingScript.sh | grep $paramTWO | sed s#\ ##g > /tmp/tempResults2.txt
并在引用脚本的其余部分中修改这些tempResults文件的路径。您会惊讶地看到他们的工作精美。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句