我已经编写了一个shell脚本来从远程服务器复制文件并将同一文件转换为另一种格式。转换后,我将使用sed命令编辑该文件。脚本在手动执行时运行成功,但在通过crontab执行时失败。shell脚本手动运行,但在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 [email protected] '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 [email protected]:/$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即可。 –