【发布时间】:2017-10-09 07:25:54
【问题描述】:
我编写了一个 shell 脚本来从远程服务器复制文件并将同一个文件转换为另一种格式。转换后,我将使用 sed 命令编辑文件。脚本在手动执行时运行成功,但在通过 crontab 执行时失败。
Crontab 条目是: */1 * * * * /script/testshell.sh
下面是shell脚本代码:
#!/bin/bash
file="/script/test_data.csv" if [ -f "$file" ] then
echo " file is present in the local machine " else
echo " file is not present in the local machine "
echo " checking the file is present in the remote server "
ssh user@IP 'if [ -f /$path ]; then echo File found ; else echo File not found; fi' fi
if [ -f "$file"] then
rm -f test_data.csv fi
scp -i /server.pem user@IP:/$path
file="/script/test_data.csv" if [ -f "$file" ] then
echo "$file found." else
echo "$file not found." fi
if [ -f "$file" ] then echo " converting csv to json format ....." fi
./csvjson.sh input.csv output.json
sed -e '/^$/d' -e 's/.*/&,/; 1 i\[' ./output.json | sed ' $ a \]'
hello.json
手动运行脚本后,它可以完美运行。但不适用于 crontab。
【问题讨论】:
-
尽量使用绝对路径
-
谢谢。现在它的工作。我到处都给出了绝对路径。
-
这是一个糟糕的主意,只需在脚本开头正确设置 PATH 即可。