2017-10-09 94 views
0

我已经编写了一个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。

+1

尝试到处 –

+0

由于使用绝对路径。现在它的工作。我给了绝对的路径无处不在。 –

+0

这是一个可怕的想法,只需在脚本开始时正确设置PATH即可。 –

回答

0

什么不从cron工作,什么是输出有任何错误从cron?

有几件事情尝试:所以如果你的脚本需要什么,从将其设置包括在crontab命令例如

的cron不运行在默认情况下您的个人资料

“ ./.bash_profile;/script/testshell.sh”

我看不到$路径设置的任何地方,虽然它的用于测试现有的文件,你设置的是手动某处因此深得不能从cron中设置?

你的一些脚本和文件被指定为当前目录(./),从cron这将是你的主文件夹,是正确的还是你需要更改脚本中的目录或使用它们的路径?

希望帮助

+0

现在cronjob工作得很好。我到处都是绝对的道路。 –

相关问题