Power consumption of a bash command
Actually you don't need to write it at all! Use Activator! You can add an activator event that runs whenever you start charging or stop charging. 

Also there is Activator plugin that allows you to run a shell script on an Activator event.

EDIT

I don't have any of my jailbroken devices with me currently, but I can get you the name of the Activator plugin.