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
and search for the *battery.rechareable.is_discharging* key. Back up so you get the block that details your battery info:
Note the udi of the device. Now, query the property you want using the hal-get-property command:
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:
Within the script, you should use the start-up scripts to cleanly shut down mysql etc
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:
You can use this command to stop or start services :
/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.