#!/bin/bash

set -u

REMOTE="carlos.bastarz@egeon.cptec.inpe.br"
SCRIPT="/mnt/beegfs/carlos.bastarz/SMNAMonitoringApp/cron_scripts/anls_img/qsub_teste3.sh"
LOG="/share/das/dist/carlos.bastarz/sandbox/SMNAMonitoringApp/cron_scripts/anls_img/teste-egeon.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" \
    "sbatch '$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
