這幾天在弄數據庫備份的事情,其中涉及到使用crontab命令自動執行shell腳本的問題,發現將寫好的數據庫導出腳本export.sh
1 ############################################################################### 2 extfile=`date +"%m_%d_%H_%M"` 3 ############################################################################### 4 exp aa/aa file=/home/oracle/expfile/aafulltmp_$extfile.dmp constraints=y owner=aa log=n 5 exp bb/bb file=/home/oracle/expfile/bbfulltmp_$extfile.dmp constraints=y owner=bb log=n
加入到crontab中會執行失敗
crontab內容如下
[dbserver@db20 ~]$ crontab -l 32 2 * * * /home/dbserver/exportdb.sh
根據crontab執行后會給用戶發送一封郵件中的提示顯示
/home/dbserver/export.sh: line 4: exp: command not found /home/dbserver/export.sh: line 5: exp: command not found
看起來不是crontab的問題,而是沒有找到exp命令,說明是環境變量的問題,crontab執行時環境變量與直接執行是不一樣的,去網上找到了解決辦法如下
在shell腳本前面加入
#!/bin/bash source /home/dbserver/.bash_profile
最終的shell文件內容為
############################################################################### extfile=`date +"%m_%d_%H_%M"` ############################################################################### #!/bin/bash source /home/dbserver/.bash_profile exp aa/aa file=/home/oracle/expfile/aafulltmp_$extfile.dmp constraints=y owner=aa log=n exp bb/bb file=/home/oracle/expfile/bbfulltmp_$extfile.dmp constraints=y owner=bb log=n
至此成功執行,還有一種方法是將exp命令寫成絕對路徑
完結!