Como desligar o sistema se não estiver carregando

1

Estou usando o Ubuntu 14.04 LTS. Minha bateria não está funcionando, ou seja, fornece backup por cerca de 5 minutos. Agora, quando eu faço o download de material, às vezes leva de 5 a 6 horas. E eu não posso simplesmente ficar com o laptop por tanto tempo. Então, eu quero fazer um código, que irá verificar se a bateria está carregando a cada 5 minutos, e se não, ele irá desligar o sistema.

    
por Shoham Debnath 03.05.2016 / 14:04

2 respostas

0

O script abaixo usa duas chamadas para dbus e um loop para pesquisar a porcentagem. Configuração muito simples e eficaz. Execute isto quando quiser desligar o laptop assim que ele for carregado

  
#!/bin/bash
get_percentage()
{
  qdbus org.gnome.SettingsDaemon.Power \
       /org/gnome/SettingsDaemon/Power \
        org.gnome.SettingsDaemon.Power.Percentage
}

shutdown_system()
{
  qdbus com.canonical.Unity  \
       /com/canonical/Unity/Session \
        com.canonical.Unity.Session.Shutdown

}

# Basically loop that waits till
# battery reaches 100%. When 100%
# reached , loop exits, and next command
# is executed, which is shutdown
while [ $(get_percentage) -ne 100   ] ;
do
  sleep 0.25
done

# Add delay or a warning message here if necessary
shutdown_system
    
por Sergiy Kolodyazhnyy 03.05.2016 / 16:39
1

Experimente este script python. Ele toma emprestado de Salvando o trabalho automaticamente quando a bateria está baixa

#!/usr/bin/env python

import subprocess
import dbus

sys_bus = dbus.SystemBus()

ck_srv = sys_bus.get_object('org.freedesktop.ConsoleKit',
                            '/org/freedesktop/ConsoleKit/Manager')
ck_iface = dbus.Interface(ck_srv, 'org.freedesktop.ConsoleKit.Manager')

stop_method = ck_iface.get_dbus_method("Stop")

battery_limit = 90  # in percent

def get_battery_percentage():

    percentage, err = subprocess.Popen([r'upower -i $(upower -e | grep BAT) | grep --color=never -E percentage | xargs | cut -d ' ' -f2 | sed s/%//
'], shell=True, stdout=subprocess.PIPE).communicate()

    return(int(percentage))

while True:

    if get_battery_percentage() <= battery_limit:

        stop_method()
    
por Android Dev 03.05.2016 / 14:20