Results 1 to 2 of 2

Thread: Run script on laptop plugged in/unplugged

  1. #1
    Join Date
    Jul 2011
    Beans
    63

    Run script on laptop plugged in/unplugged

    There are a couple programs that I would like to start in the background (mainly virtual machines) when my laptop is plugged in, and then paused when the laptop is unplugged/suspended/hibernated. Is it possible to run scripts that react to these power configuration changes?

  2. #2
    Join Date
    Jul 2011
    Beans
    63

    Re: Run script on laptop plugged in/unplugged

    OK for anyone out there who's actually come across this thread hoping for some actual answers, here's what I've come up with. It's only been tested on my laptop, but I see no reason why it wouldn't work with others as well.

    Code:
    #!/bin/bash
    
    # The threshold in percentage of when to consider the battery charged.
    charged="90"
    # The time in seconds between checks
    sleeptime="30"
    
    
    function loop(){
    
    
            while true
            do
                    state=$(acpi -b|sed 's/Battery 0: Unknown, //g'|sed 's/%//g') # I know it's not pretty, but it works
                    if [ "$state" -gt "$charged" ]
                    then
                            turnon
                    else
                            turnoff
                    fi
                    sleep $sleeptime
            done
    }
    
    
    function turnon(){
            # Turn stuff on here
    }
    
    
    function turnoff(){
            # Turn stuff off here
    }
    
    
    loop &
    What the above script does is checks to see if the battery is below a certain percentage, in this case 90%, and turns things off when below that threshold and turns stuff back on when above the threshold.
    Last edited by JaySeeJC; May 24th, 2013 at 03:05 PM. Reason: Added script description

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •