Skip to content

Instantly share code, notes, and snippets.

@medeirosinacio
Last active April 15, 2024 17:43
Show Gist options
  • Save medeirosinacio/3a731921ab62b08b175ef782fe63eea7 to your computer and use it in GitHub Desktop.
Save medeirosinacio/3a731921ab62b08b175ef782fe63eea7 to your computer and use it in GitHub Desktop.
MacOs Battery Alarm Script
#!/bin/bash
## display notification "Battery has disconnected with less than % battery" with title "Battery Alarm"
function debug() {
logger "$1"
echo "$1"
}
debug "CRONTAB | Battery Alarm Service Started"
charging_level=$(pmset -g batt | grep -Eo "\d+%" | cut -d% -f1)
charging_status=$(pmset -g batt | awk '/Now drawing from/ {print $4}')
debug "CRONTAB | Current Battery Level: $charging_level%"
debug "CRONTAB | Current Battery Status: $charging_status"
if ! [ "$charging_status" == "'AC" ]; then
debug "CRONTAB | Battery is disconnected with $charging_level% battery"
osascript -e "display notification \"Battery is disconnected with $charging_level% battery\" with title \"Battery Alarm\""
fi
@medeirosinacio
Copy link
Author

Instruções para agendar no crontab

Para agendar a execução do script no crontab, siga estas etapas:

  1. Abra o terminal.
  2. Execute o comando crontab -e para editar o crontab.
  3. Adicione a linha a seguir para agendar a execução do script. Substitua <caminho_para_o_script> pelo caminho absoluto do seu script.
* * * * * /bin/bash <caminho_para_o_script>
  1. Salve e feche o arquivo do crontab.

Isso irá agendar a execução do script para ser executado a cada minuto. Você pode ajustar a programação conforme necessário.

Como debugar

Para debugar a execução do script usando o comando log, siga estas etapas:

  1. Abra o terminal.
  2. Execute o comando a seguir para visualizar os logs relacionados ao script nos últimos 1 minuto:
log show --last 1m | grep CRONTAB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment