#!/bin/bash

set -u

REMOTE="carlos_bastarz@login-xc50.cptec.inpe.br"
SCRIPT="/lustre_xc50/carlos_bastarz/SMNAMonitoringApp/cron_scripts/anls_img/qsub_teste3.sh"
LOG="/share/das/dist/carlos.bastarz/sandbox/SMNAMonitoringApp/cron_scripts/anls_img/teste-xc50.log"

timestamp() {
    date '+%Y-%m-%d %H:%M:%S'
}

echo "[$(timestamp)] Iniciando submissão" >> "$LOG"

if ssh \
    -o BatchMode=yes \
    -o ConnectTimeout=30 \
    -o ServerAliveInterval=30 \
    -o ServerAliveCountMax=3 \
    "$REMOTE" \
    "/opt/pbs/default/bin/qsub '$SCRIPT'" >> "$LOG" 2>&1
then
    echo "[$(timestamp)] Submissão realizada com sucesso" >> "$LOG"
else
    rc=$?
    echo "[$(timestamp)] ERRO: qsub remoto falhou (status $rc)" >> "$LOG"
    exit "$rc"
fi
