2017-10-09 165 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工作得很好。我到處都是絕對的道路。 –

相關問題