Skip to main content
Answer

Running a policy only if the macbook air is plugged in... ( pmset -g ps )

  • January 16, 2012
  • 6 replies
  • 27 views

rob_potvin
Forum|alt.badge.img+26

I am running the following command on the mac and I want to create a policy that would only run if the computer is plugged in

pmset -g ps | awk 'NR>1{exit};1'

This gives me the current power status and only the 1st line (don't want the battery info and percent)

Example I get this

Currently drawing from 'AC Power'

or

Currently drawing from 'Battery Power'

What I want is just the words AC Power or Battery Power

and then do an if statement

if Battery Power exit

if AC Power run update!

Can you guys give me a hand, my grep and awk skills suck,

Thanks guys

Best answer by stevewood

This should give you what you are looking for. As always, TEST, TEST, TEST.

pmset -g ps | awk 'NR>1{exit};1' | awk '{print $4,$5}' | sed "s/'//g"

Steve

6 replies

stevewood
Forum|alt.badge.img+38
  • Hall of Fame
  • Answer
  • January 16, 2012

This should give you what you are looking for. As always, TEST, TEST, TEST.

pmset -g ps | awk 'NR>1{exit};1' | awk '{print $4,$5}' | sed "s/'//g"

Steve


rob_potvin
Forum|alt.badge.img+26
  • Author
  • Employee
  • January 16, 2012

Thanks works perfect,

GOING TO TEST TEST TEST! Thanks :-)

Also going to start a hacking your statement apart...


Forum|alt.badge.img+12
  • Contributor
  • January 17, 2012

This can be really short. You only need to know if it exists, there's no need to analyse the actual sentence. Try this, it basically says if $isAC isn't empty then run something.

isAC=pmset -g ps | grep "AC Power"

if [[ $isAC ]]

then

echo "AC DO THIS"

fi

Alternatively, you could use =~ as a 'does it contain this':

isAC=pmset -g ps | grep "^Currently drawing"

if [[ "$isAC" =~ "AC" ]]

then

echo "AC DO THIS"

fi


rob_potvin
Forum|alt.badge.img+26
  • Author
  • Employee
  • January 17, 2012

Awesome thanks...


Forum|alt.badge.img+31
  • Honored Contributor
  • January 17, 2012

Are you using a manual trigger policy?


rob_potvin
Forum|alt.badge.img+26
  • Author
  • Employee
  • January 17, 2012

Yeah the macbook airs have this thunderbolt update that needs to go out and they need to be plugged in for that, so basically the logic is

Open Self Service

Login

Click Update Me / checks to see if plugged in / opens window telling students to leave computer plugged / computer does the update plus we have this iMovie issue where they can't see the iMovie window that will be fixed also / then restarts

if not plugged in, policy fails with a window telling the student to run this again when computer is plugged in.

Makes sense ??