Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di Guglie
    Registrato dal
    Dec 2002
    Messaggi
    1,572

    [howto] - spegnere il portatile quando la batteria sta per finire

    ci sono diversi programmi che tengono sotto controllo lo stato della batteria e che eseguono comandi quando la batteria è troppo scarica, ad esempio gtkmm e xfce4-battery, ma non li ho voluti usare perchè ogni tanto mi capita di usare il portatile senza X avviato

    il mio metodo è distinto in 3 parti:

    1. uno script bash
    2. un programmino in c++ e gtkmm
    3. l'istruzione da inserire in cron

    1. script bash
    ho deciso quindi di scrivere un piccolo scriptino in bash che controlla lo stato della batteria, nel mio caso con acpi; quando la batteria scende sotto la soglia WARNING_PERCENT emette un suono di avvertimento e cerca di fare partire sul display 0 un programmino in C++ che chiede se si vuole spegnere il portatile (in caso affermativo killa X e spegne il pc)
    per spegnere il pc bisogna avere installato sudo e avere assegato all'utente che usa X il permesso di usare shutdown senza password (so che è pericoloso, ma lo preferisco a lanciare un programma su X scrotto da me con root)

    codice:
    #!/bin/bash
    #file: /usr/bin/check_battery_state
    
    WARNING_PERCENT=4
    REBOOT_PERCENT=2
    
    MUSIC_PLAYER="aplay"
    PATH_TO_WARNING_WAV="/usr/share/sounds/alarms/siren.wav"
    PATH_TO_ALARM_WAV="/usr/share/sounds/alarms/gong.wav"
    
    SECONDS_BEFORE_REBOOT=10
    
    function start_halt()
    {
    	killall X
    	sudo /sbin/shutdown -h now
    }
    
    # This part of the script use acpi on my laptop: change if needed
    STATE=`cat /proc/acpi/battery/BAT0/state | grep "charging state:" | awk '{print $3}'`
    PURCENT=`/usr/bin/get_battery_percent | bc -l`
    
    
    if [ "$STATE" = "discharging" ];
    then
    	if [ $PURCENT -lt $REBOOT_PERCENT ];
    	then
    		$MUSIC_PLAYER $PATH_TO_ALARM_ALARM
    		sleep $SECONDS_BEFORE_REBOOT
    		start_halt
    	elif [ $PURCENT -lt $WARNING_PERCENT ];
    	then
    		$MUSIC_PLAYER $PATH_TO_WARNING_WAV &
    		#NOTE: ask_windows is a small c++ program written from me that ask
    		#       if the user will really reboot the machine
    		REBOOT=`DISPLAY=:0 ask_window`
    		
    		if [ $REBOOT = 1 ];
    		then
    			sleep 2
    			start_halt
    		fi
    	fi
    fi
    lo script è da copiare in /usr/bin/check_battery_state

    2. programmino in C++ e gtkmm
    il programmino in C++ non è niente di speciale, potete scaricare i sorgenti qui (non posto tutto il codice perchè sarebbe un po' troppo lungo)
    l'unica dipendenza dovrebbe essere gtkmm-2.4 (il porting di gtk-2.4 per c++)

    per chi non volesse installare questo programmino basta commentare nello script di prima le linee
    codice:
    REBOOT=`DISPLAY=:0 ask_window`
    if [ $REBOOT = 1 ];
    then
    	sleep 2
    	start_halt
    fi
    in maniera da sentire solamente il suono di avviso, oppure scrivere un altro programmino che abbia come output sulla shell 1 se l'utente sceglie di rebootare o 0 se sceglie il contrario

    3. istruzione per cron
    avrò provato non so quanti modi diversi per richiamare check_battery_state da /etc/crontab e non ci sono riuscito..
    l'unica soluzione che ho trovato è stata quella di usare il crontab dell'utente che usa X (da me non è un problema perchè il portatile lo uso solo io) con
    codice:
    # crontab -u nome_utente -e
    codice:
    */1 * * * * /usr/bin/check_battery_state
    in questo modo lo script viene eseguito ogni minuto


    è tutto
    powered by GNU/Linux Gentoo
    A Elbereth Gilthoniel o menel palan-diriel, le nallon sí di-nguruthos! A tiro nin, Fanuilos!

  2. #2
    Utente di HTML.it L'avatar di Guglie
    Registrato dal
    Dec 2002
    Messaggi
    1,572
    ho scritto 2 ebuilds:
    sys-power/check_battery_state ha come dipendenza x11-misc/ask_window, se compilato con la use flag +X:
    se volete provarli basta mettergli nell'overlay ed emergere check_battery_state

    inoltre ho scritto meglio ask_window: ora visualizza anche lo stato della batteria e il tempo residuo della batteria

    check_battery_state-0.1.ebuild
    codice:
    # Copyright 1999-2005 Gentoo Foundation
    # Distributed under the terms of the GNU General Public License v2
    # $Header: $
    
    DESCRIPTION="Script to keep under control the battery-power"
    HOMEPAGE="http://web.ticino.com/guglie/dev/sh.html"
    SRC_URI="http://web.ticino.com/guglie/pub/dev/sh/battery/${P}.tar.bz2"
    
    LICENSE="GPL-2"
    SLOT="0"
    KEYWORDS="x86"
    
    DEPEND="X? ( x11-misc/ask_window )"
    RDEPEND="${DEPEND}"
    
    IUSE="X"
    
    src_unpack()
    {
    	unpack ${A}
    }
    
    src_install()
    {
    	dodir /usr/share/sounds/alarms
    	cp alarms/gong.wav ${D}usr/share/sounds/alarms/
    	cp alarms/siren.wav ${D}usr/share/sounds/alarms/
    
    	dodir /usr/bin
    	cp get_battery_percent.sh ${D}usr/bin/get_battery_percent
    	cp get_battery_time.sh ${D}usr/bin/get_battery_time
    	cp check_battery_state.sh ${D}usr/bin/check_battery_state
    	einfo "IMPORTANT"
    	einfo "if you would check every minute the state of the battery add the following line the crontab of your user"
    	einfo
    	einfo "*/1 * * * * /usr/bin/check_battery_state"
    }
    ask_window-0.2.ebuild
    codice:
    # Copyright 1999-2005 Gentoo Foundation
    # Distributed under the terms of the GNU General Public License v2
    # $Header: $
    
    DESCRIPTION="Small software that ask if the user will reboot his machine"
    HOMEPAGE="http://web.ticino.com/guglie/dev/c++.html"
    SRC_URI="http://web.ticino.com/guglie/pub/dev/c++/gtkmm/${PN}/${P}.tar.bz2"
    
    LICENSE="GPL-2"
    SLOT="0"
    KEYWORDS="x86"
    
    DEPEND=">=dev-cpp/gtkmm-2.4"
    RDEPEND="${DEPEND}"
    
    src_unpack()
    {
    	unpack ${A}
    }
    
    src_compile()
    {
    	emake || die "emake mailed"
    }
    
    src_install()
    {
    	dodir /usr/local/${PN}
    	cp ${PN} ${D}usr/local/${PN}
    	cp -R img/ ${D}usr/local/${PN}
    	dodir /usr/local/bin
    	cp launcher.sh  ${D}usr/local/bin/${PN}
    }
    powered by GNU/Linux Gentoo
    A Elbereth Gilthoniel o menel palan-diriel, le nallon sí di-nguruthos! A tiro nin, Fanuilos!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.