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

rob_potvin
Contributor III
Contributor III

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

2 ACCEPTED SOLUTIONS

stevewood
Honored Contributor II
Honored Contributor II

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

View solution in original post

sean
Valued Contributor

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

View solution in original post

6 REPLIES 6

stevewood
Honored Contributor II
Honored Contributor II

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
Contributor III
Contributor III

Thanks works perfect,

GOING TO TEST TEST TEST! Thanks :-)

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

sean
Valued Contributor

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
Contributor III
Contributor III

Awesome thanks...

tlarkin
Honored Contributor

Are you using a manual trigger policy?

rob_potvin
Contributor III
Contributor III

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 ??