Skip to content

Instantly share code, notes, and snippets.

@coffnix
Created December 10, 2018 14:00
Show Gist options
  • Save coffnix/bffb0a9e85c02455ed0a2484dadfbde5 to your computer and use it in GitHub Desktop.
Save coffnix/bffb0a9e85c02455ed0a2484dadfbde5 to your computer and use it in GitHub Desktop.
lock em bash script
#!/bin/bash
###############################################################################
# Testa se script já encontra-se em execução
test_running() {
RUNNING=$(ps aux | grep "$(printf '%s\n' "${0##*/}")" | grep bash | grep -v "bash -c" | grep -v $$ | grep -v grep | wc -l)
if [ ${RUNNING} -gt 1 ]; then
echo "Script ja esta rodando, aguardando 10 segundos para tentar novamente."
sleep 10
RUNNING=$(ps aux | grep "$(printf '%s\n' "${0##*/}")" | grep bash | grep -v "bash -c" | grep -v $$ | grep -v grep | wc -l)
if [ ${RUNNING} -gt 1 ]; then
echo "Script ja esta rodando, nao sera executado novamente."
exit 1
fi
fi
}
test_running
###############################################################################
# Iniciando script
sleep 30
###############################################################################
# Fim do script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment