使用shell脚本远程设置crontab。

时间:2015-10-20 13:49:55

标签: bash shell echo ksh

我正在尝试编写一个代码,该代码将使用shell脚本远程设置crontab。但我面临着echo命令的问题,因为它无法替换变量。

实际代码

#! /usr/bin/ksh
#! /bin/bash
clear

echo -n "" > error.out
echo -n "" > scripts_dir.out
echo -n "" > crontab_check.out


export ORACLE_SID=REPTEST
export ORACLE_HOME=/opt/app/oracle/product/11.2.0_tipical
export PATH=$PATH:$ORACLE_HOME/bin
export TNS_ADMIN=/home/oracle/NIKHIL/apex_oracle_version_changes
export SCRIPTS_DIR=/home/oracle/NIKHIL/Migration_tech_refersh
export REP_USER=repository
export EMAIL_ID="nikhil.mehta@neustar.biz"
export DAY=`date +"%m-%d-%Y"` 
export HOSTNAME=$1
export SID=$2
export USAGE=$3
export ROLE=$4


create_crontab_entries() {

    echo -n "" > ${SCRIPTS_DIR}/crontab_remote_entries/crontab_remote_file_${H_NOFQDN}.${SID}
    echo
    echo -e "Creating crontab entries for RMAN backups and alert_log script"
    echo
    mv ${SCRIPTS_DIR}/crontab_remote_entries/crontab_remote_file_${H_NOFQDN}.${SID} ${SCRIPTS_DIR}/crontab_remote_entries/crontab_remote_file_${H_NOFQDN}.${SID}_$DAY
    echo -e "(crontab -l 2>/dev/null;echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/oracle/SCRIPTS/MONITOR/alert_log/ksh/CHECK_LOG_ERRORS.ksh ${SID} > /tmp/check_log_${SID}.log 2>&1") |  crontab - " >> ${SCRIPTS_DIR}/crontab_remote_entries/crontab_remote_file_${H_NOFQDN}.${SID}

    echo -e "Checking if crontab is enabled for oracle user"
    ssh -o 'PreferredAuthentications=publickey' oracle@${HOSTNAME} "crontab -l" >> crontab_check.out 2>&1

    if [ "$?" = "0" ]; then
            echo
            echo -e "Crontab is enabled for Oracle user.. Creating Crontab entries!!!"
            scp  -o 'PreferredAuthentications=publickey' ${SCRIPTS_DIR}/crontab_remote_entries/crontab_remote_file_${H_NOFQDN}.${SID} oracle@${HOSTNAME}:/home/oracle >> crontab_check.out 2>&1
            ssh -o 'PreferredAuthentications=publickey' oracle@${HOSTNAME} "chmod 777 /home/oracle/crontab_remote_file_${H_NOFQDN}.${SID};/home/oracle/crontab_remote_file_${H_NOFQDN}.${SID};rm -f /home/oracle/crontab_remote_file_${H_NOFQDN}.${SID}" >> crontab_check.out 2>&1

Problamatic echo命令

echo -e "(crontab -l 2>/dev/null;echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/oracle/SCRIPTS/MONITOR/alert_log/ksh/CHECK_LOG_ERRORS.ksh ${SID} > /tmp/check_log_${SID}.log 2>&1") |  crontab - " >> ${SCRIPTS_DIR}/crontab_remote_entries/crontab_remote_file_${H_NOFQDN}.${SID}

运行脚本时遇到的错误

Migration_tech_refersh_post_check.ksh: line 275: 1) |  crontab - : ambiguous redirect

请帮助echo命令,它将按预期替换变量。

1 个答案:

答案 0 :(得分:0)

我能够通过将整个命令分成3个字符串来解决问题,如下所示。

    str1='(crontab -l 2>/dev/null;echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/oracle/SCRIPTS/MONITOR/alert_log/ksh/CHECK_LOG_ERRORS.ksh '
    str2="$str1 $SID > /tmp/check_log_${SID}.log 2>&1"
    str3='") |  crontab - '
    full_cmd1="$str2$str3"

感谢大家的帮助。

此致 Lancerique。