Last active
November 12, 2018 11:27
-
-
Save vndmtrx/5e09f70d6d041d89894fcb98a9a6c044 to your computer and use it in GitHub Desktop.
Script para execução de atividades no Cron, com controle de horas e bloqueio de múltiplas execuções. Rode com cron_script.sh <path da atividade>.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## Script Modelo de execução de tarefas no cron | |
## Recomendação: Usar script na pasta /etc/cron.d/ em vez de usar cron de usuário (crontab -e) | |
## Recomendação: Usar rotação de logs (https://www.thegeekstuff.com/2010/07/logrotate-examples) | |
##################################################### Configuração #################################################### | |
#TODO: Criar /etc/logrotate.d/script | |
if [ $# -ne 1 ]; then | |
echo "Uso: $0 <path do script>" | |
exit 1 | |
else | |
SCRIPT_PATH=$1 | |
SCRIPT_NOME="$(basename ${SCRIPT_PATH})" | |
fi | |
SCRIPT_LOG=/var/log/cron-${SCRIPT_NOME}.log | |
touch ${SCRIPT_LOG} | |
############################################## Rotinas de Lock do Script ############################################## | |
SCRIPT_LOCK=/tmp/${SCRIPT_NOME}.lock | |
PIDFILE=${SCRIPT_LOCK}/PID | |
UNLOCK_V=0 | |
P1=-1 | |
echo "PID: $$." | |
function lock { | |
mkdir ${SCRIPT_LOCK} &> /dev/null | |
if [ $? == 0 ]; then | |
echo $$ > ${PIDFILE} | |
echo "Lock." | |
else | |
PID=$(cat $PIDFILE) | |
echo "Lock detectado. PID do Lock: ${PID}" | |
kill -0 ${PID} &>/dev/null | |
if [ $? != 0 ]; then | |
echo "Processo ${PID} inexistente. Limpando." | |
unlock | |
echo "Reiniciando script." | |
exec "$0" "$@" | |
else | |
echo "Outra instância rodando. Abortando." | |
UNLOCK_V=1 | |
exit 1 | |
fi | |
fi | |
} | |
function unlock { | |
echo "Unlock." | |
rm -rf ${SCRIPT_LOCK} | |
} | |
function exit_end { | |
echo "Exit." | |
if [ ${UNLOCK_V} == 0 ]; then | |
unlock | |
UNLOCK_V=1 | |
fi | |
if [ ${P1} != -1 ]; then | |
kill -TERM "${P1}" &>/dev/null | |
fi | |
exit $1 | |
} | |
trap 'exit_end $?' KILL ERR QUIT TERM EXIT INT | |
lock | |
######################################################### Log ######################################################### | |
# Registro de início da atividade | |
INICIO_T="$(date +'%d-%m-%Y às %H:%M:%S')" | |
echo "####################################" >> ${SCRIPT_LOG} | |
echo "### Execução agendada do script ${0}." >> ${SCRIPT_LOG} | |
echo "### Início: $INICIO_T" >> ${SCRIPT_LOG} | |
echo "------------------------------------" >> ${SCRIPT_LOG} | |
time ( | |
####################################################### Script ######################################################## | |
${SCRIPT_PATH} | |
####################################################### Script ######################################################## | |
) &>> ${SCRIPT_LOG} & | |
P1=$! | |
wait ${P1} | |
P1=-1 | |
echo "Carga: $(cut -d ' ' -f1,2,3 /proc/loadavg)" >> ${SCRIPT_LOG} | |
# Registro de fim da atividade | |
FIM_T="$(date +'%d-%m-%Y às %H:%M:%S')" | |
echo "------------------------------------" >> ${SCRIPT_LOG} | |
echo "### Término: ${FIM_T}." >> ${SCRIPT_LOG} | |
echo "####################################" >> ${SCRIPT_LOG} | |
echo >> ${SCRIPT_LOG} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
sleep 2 | |
echo 1 | |
sleep 2 | |
echo 2 | |
sleep 2 | |
ls -la | |
sleep 2 | |
echo 3 | |
sleep 2 | |
echo 4 | |
sleep 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment