I was wondering if it is possible to set up profiles for my ubuntu laptop such that:
- Disable apache/mysql/postgresql if running on batterymode
- Disable all processes that are attempting to connect to the internet, if wireless is disabled.
I haven't come across the concept of such a profile in Ubuntu before, so I'm guessing that you can't do this right off the bat.
I could be wrong in assuming this, which would be nice. However, assuming there isn't an app to do this, and I had a strong desire to set this up, here's now I'd go about it.
I'm going to write this with the assumption you're running Gnome, since that's the environment I know best. The underlying process flow should still hold valid if you're in some other desktop or console environment.
Gnome lets you configure some aspects of system behaviour via System > Preferences > Power Management. This calls gnome-power-preferences, and it is enforced by the gnome-power-manager daemon process. From the gnome project page we know that gnome-power-manager depends on HAL to know if the laptop is operating on battery power.

So, fire up gnome-device-manager to see what HAL knows. Find the entry for battery. If you only see a summary tab, enable View > Device Properties. Yup, there are the two keys that we need: battery.rechargeable.is_discharging and battery.rechargeable.is_charging

Now we know what we need, we have to find out how to access this via the command line. Helpfully HAL comes with command-line tools to access this information. We first run
hal-device | less
and search for the battery.rechareable.is_discharging key. Back up so you get the block that details your battery info:
56: udi = '/org/freedesktop/Hal/devices/computer_power_supply_battery_BAT0'
linux.subsystem = 'power_supply' (string)
info.capabilities = { 'battery' } (string list)
info.subsystem = 'power_supply' (string)
info.product = 'DELL CC1546' (string)
info.udi = '/org/freedesktop/Hal/devices/computer_power_supply_battery_BAT0' (string)
battery.type = 'primary' (string)
battery.reporting.technology = 'Li-ion' (string)
battery.technology = 'lithium-ion' (string)
battery.model = 'DELL CC1546' (string)
battery.vendor = 'Panasonic' (string)
battery.voltage.design = 11100 (0x2b5c) (int)
battery.voltage.unit = 'mV' (string)
battery.reporting.design = 4700 (0x125c) (int)
battery.reporting.unit = 'mAh' (string)
battery.serial = '1076' (string)
battery.present = true (bool)
battery.voltage.current = 12712 (0x31a8) (int)
battery.reporting.rate = 2765 (0xacd) (int)
battery.is_rechargeable = true (bool)
battery.rechargeable.is_charging = true (bool)
battery.rechargeable.is_discharging = false (bool)
battery.reporting.current = 3407 (0xd4f) (int)
battery.reporting.last_full = 3963 (0xf7b) (int)
battery.charge_level.current = 37817 (0x93b9) (int)
info.parent = '/org/freedesktop/Hal/devices/computer' (string)
linux.sysfs_path = '/sys/devices/LNXSYSTM:00/device:00/PNP0C0A:00/power_supply/BAT0' (string)
battery.charge_level.design = 52170 (0xcbca) (int)
battery.charge_level.percentage = 85 (0x55) (int)
battery.remaining_time = 723 (0x2d3) (int)
battery.charge_level.rate = 30691 (0x77e3) (int)
battery.charge_level.last_full = 43989 (0xabd5) (int)
info.category = 'battery' (string)
linux.hotplug_type = 2 (0x2) (int)
Note the udi of the device. Now, query the property you want using the hal-get-property command:
$ hal-get-property --udi /org/freedesktop/Hal/devices/computer_power_supply_battery_BAT0 --key "battery.rechargeable.is_discharging"
true
Now we know how to access the battery status, stick this in a daemon-like script. Below is a basic template that takes the polling interval as the first parameter. You can fancy it up with error-checking and other nice-to-have constructs at leisure:
#!/usr/bin/env perl
my ($sleep_duration) = @ARGV;
print "Sleep:$sleep_duration\n";
do{
my $status = qx(hal-get-property --udi /org/freedesktop/Hal/devices/computer_power_supply_battery_BAT0 --key "battery.rechargeable.is_discharging");
if ($status =~ /true/)
{
print "On battery power\n";
# shut down apache
# shut down mysql
# shut down postgresql
}
sleep $sleep_duration;
} while (true);
Within the script, you should use the start-up scripts to cleanly shut down mysql etc
sudo /etc/init.d/mysqld stop
How can we improve on this? Once you have run this for a while and ironed out all the kinks, make this a service that starts up at boot. Check this answer here for instructions on adding a script to start automatically at boot-up.
If you decide not to do this, check this question for instructions on how to let this script execute commands that require root permissions.
I realize I have only answered one portion of your question, but you should be able to incorporate the wireless section in similar fashion. Use HAL to understand when wireless is disabled.
I'm not exactly certain how you check and disable all processes that try to connect to the net, but the following commands might help you with that task:
# list all processes using port 80
lsof -i tcp:80
#list programs connected via tcp
#include -u to include udp connections
sudo netstat -ntp
laptop-mode-tools can do what you're asking for.
/etc/laptop-mode/conf.d/start-stop-programs.conf
The start-stop-programs module allows you to start or stop programs
when the computer switches to a different power state.
CONTROL_START_STOP
If this option is enabled, laptop mode tools will automati‐
cally start and stop daemons or other programs for you. The
actual configuration of which daemons are to be
stopped/started is done by placing links to the daemons’ init
scripts in the following directories:
/etc/laptop-mode/batt-start
/etc/laptop-mode/batt-stop
/etc/laptop-mode/lm-ac-start
/etc/laptop-mode/lm-ac-stop
/etc/laptop-mode/nolm-ac-start
/etc/laptop-mode/nolm-ac-stop
As you have probably guessed, the directories of the form "X-
stop-daemons" should contain init scripts of daemons that you
want stopped in mode X, while the directories of the form "X-
start-daemons" should contain init scripts of daemons that
you want started in mode X. Of course, it is possible to put
in your own handling of modes as well: the only requirement
on the scripts in the directories is that they handle the
"start" and "stop" commands, like init scripts usually do.
The ordering of the script handling is as follows. When a
mode is entered, the actions of the previous mode are undone,
in reverse order. This means that if the previous mode had
done "daemon1 stop", "daemon2 stop" and "daemon3 start", then
the undoing actions will be "daemon3 stop", "daemon2 start",
"daemon1 start". After that, the stop-scripts for the new
mode are called, and then the start-scripts are called.
Please note that there is no detection of commonalities
between modes at this point, i.e., if the mode you’re coming
from and the mode you’re going to both specify that a daemon
"X" should be stopped, then the daemon will be un-stopped
(that is, started) while leaving the previous mode, and then
stopped again.
BATT_STOP
BATT_START
LM_AC_STOP
LM_AC_START
NOLM_AC_STOP
NOLM_AC_START
These options allow you to stop services (through their init
scripts) in certain power states. Specify a space-separated
list of service names in these options. These services are
started/stopped together with the files from the directories
mentioned above.
It is possible to disable it's main function (spinning down HDD) from config file, it will still run other actions.
You can use this command to stop or start services :
sudo /etc/init.d/'preferred services' stop|start|restart