使用cron scheduler调用shell脚本时,它不起作用

时间:2017-08-23 11:09:47

标签: linux bash shell ubuntu

我有这个sh脚本

SPTH="/home/db_backup/log/log_"$(date +'%d-%m-%Y %H:%M')".txt"
echo "===DUMP started at $(date +'%d-%m-%Y %H:%M:%S')===" >> "$SPTH";
DBNAME="test"

PGPASSWORD=postgres psql -U postgres -d postgres -c "CREATE DATABASE $DBNAME";
PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "ALTER SCHEMA public RENAME TO public_old";
for entry in /home/SQL/*.*
do
    echo "$entry"
    SUBSTRING=$(echo $entry| cut -d'/' -f 4)
    #echo $SUBSTRING
    SCHEMA=$(echo $SUBSTRING| cut -d'.' -f 1)
    echo $SCHEMA
    echo "===================== CREATING public SCHEMA =====================" >> "$SPTH";
    PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "Create Schema public";
    echo "===================== RESTORING DATA OF $SCHEMA =====================" >> "$SPTH";
    PGPASSWORD=postgres psql -U postgres -d $DBNAME -f "$entry";
    echo "===================== DELETING SCHEMA $SCHEMA=====================" >> "$SPTH";
    PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "DROP SCHEMA $SCHEMA CASCADE";
    echo "===================== RENAMING SCHEMA PUBLIC TO $SCHEMA=====================" >> "$SPTH";
    PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "ALTER SCHEMA public RENAME TO $SCHEMA";
    echo "===================== RENAMING SQL FILE =====================" >> "$SPTH";
    mv /home/SQL/{$SUBSTRING,DONE--$SUBSTRING};
    echo "===================== DONE =====================" >> "$SPTH";
done
PGPASSWORD=postgres psql -U postgres -d $DBNAME -c "ALTER SCHEMA public_old RENAME TO public";
echo "===DUMP finished at $(date +'%d-%m-%Y %H:%M:%S')===" >> "$SPTH";

当我使用命令

运行此命令时
sh script.sh

它的工作正常,按照我的预期运行并运行每个命令。但是当我使用cron调度程序运行此脚本时,与POSTGRES相关的命令没有执行。我还希望在这个shell脚本中记录每个命令。

我想使用cron scheduler运行此脚本。

我哪里错了?

0 个答案:

没有答案