I have some scripts which try to do backups on the laptop. It is a little more involved, since the laptop lid is closed most of the time, including at night. So the strategy is to have a cron job start periodically, determine if a backup had been done today and if not, attempt to do one.
Since the laptop could be in various locations, it tries to determine where it is – i.e. if it is in a known location (my house or my sister’s house, or one of my DC friends houses). If so, it can back up accordingly. This is done using SSIDs, which is adequate if not elegant.
One issue I’ve had is if the attempted backup starts right after the lid is opened, the wireless may not be ready yet, and so the backup may fail on account of no network. So I set out to try to figure out how to detect when the network is available, so the script can obtain that information.
I’ve created a script that runs when I get a “lid event”, i.e. the lid opens or closes. This is based mostly on instructions in https://help.ubuntu.com/community/LaptopLidAndDockScripts. The approach is to add an event to /etc/acpi/events called lm_lid which fires on the button/lid event and runs a script, which I have in /usr/local/bin/lid_event. That script checks the state of the lid in /proc/acpi/button/lid/LID0/state and can make the information available.
When the lid opens and closes I’m playing a sound, as the example at the above link shows. And as the example explains, to do that I have to capture the X information for the user that is logged in, which should be me. In fact, the lid script will simply exit if user dee isn’t logged in (because the home directory is encrypted, and if I’m not logged in the system can’t read my home directory). There is another script in /usr/local/bin/exportXinfo which attempts to gather the X information for user dee whenever I log in, and publish that information so the lid script can use it to play the sound in the environment of user dee.
So back to the original problem, when the script processes a lid open event, it then waits forĀ the wireless device to be present (by checking /sys/class/net/<wireless device>, and then waits a few seconds, if necessary, for carrier to appear on the wireless (by checking /sys/class/net/<wireless device>/carrier). Once carrier shows up it plays an ok sound and enables the backup script to be run.