2014-01-10 37 views
0

我有一個如下所示的shell腳本。當我手動運行它時,輸出會附加到文件中。當我安排腳本從crontab運行時,輸出不會附加到輸出文件。從shell腳本運行時,sqlplus查詢不會將結果保存到文件

#!/bin/bash 
cd /home/sample 

echo "SELECT count (*) FROM Mytable;"| sqlplus -L user/[email protected] > sample.txt 

echo "SELECT name, age count(*) from mytable "| sqlplus -L lmsuser/[email protected] > sample1.txt 
+0

你的'crontab'的內容是什麼? –

+0

57 * * * * /data/sample.sh >> /data/sample.log – user2306367

+0

因此,應該輸出哪個文件到 - 'sample1.txt',就像shell腳本命令或/ data/sample一樣。日誌「在crontab中?我懷疑輸出到'sample1.txt',而你期待它在'/ data/sample.log'中。 –

回答

0

Cron做了有趣的事情與輸出,因爲它明確地沒有連接到任何人會看。

cron then wakes up every minute, examining all stored crontabs, check- 
    ing each command to see if it should be run in the current minute. 
    When executing commands, any output is mailed to the owner of the 
    crontab (or to the user named in the MAILTO environment variable in the 
    crontab, if such exists). The children copies of cron running these 
    processes have their name coerced to uppercase, as will be seen in the 
    syslog and ps output. 

我不知道這發生在哪裏;它可能會影響標準。

檢查您的電子郵件,看看輸出是否已被重定向到那裏。

0

我正面臨同樣的問題。它通常發生是因爲你的cron不知道你在.profile中初始化的變量。當您通過cron運行此腳本時,即使路徑爲sqlplus也不可用。所以,要解決這個問題,請將cron作業設置爲以下格式,並且它可以正常工作。

00 * * * * . ~/.profile && /home/absolut_path_to_script.sh > /home/log_file_path.txt 

P.S.我用過了。在.profile前面,以便加載相同。它解決了我的問題,並且cron正在生成所需的輸出。

相關問題