With iControl app for jailbroken iPhones, you can optimize battery life of your phone and do it so automatically.

Official description:

iControl automatically manages the state of your WiFi, 3G, Bluetooth and 2G radios to maximize the battery life of your iPhone. iControl also extends battery life by allowing you to designate app that iControl should kill after you lock your phone.

Once preferences have been set, iControl will ensure that the settings are preserved every time you lock/unlock your phone. Settings include setting WiFi / Bluetooth / 3G to be on only when phone is unlocked or charging and setting which potential battery-sucking applications to kill once you lock your iPhone (because certain apps require WiFi/3G to be on, even when phone is locked).