Sleep and Wake Scripting OSX

There have been many times where I thought to myself — I wish my computer would do X when it goes to sleep and Y when it wakes up. There are many applications for this, some include:

  • Mounting an SSH filesystem depending on WiFi connection
  • Have the computer greet you and dismiss you (very nerdy)
  • Disconnect/Kill some application that always hangs on sleep
  • Move files from one folder to another
  • Tweet

So how is this nerdy matter accomplished? It’s actually pretty trivial. Familiar with bash? Then you’re on the way to sleep/wake success!

Installing Sleepwatcher

Head over to http://www.bernhard-baehr.de/ and download a copy of Sleepwatcher 2.2. The installation instructions are pretty simple:

$ sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8
$ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin
$ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8

Once installed, run it in daemon mode:

$ /usr/local/sbin/sleepwatcher -d --sleep /path/to/your/sleepscript --wakeup /path/to/your/wakeupscript

If testing is needed run it in verbose mode:

$ /usr/local/sbin/sleepwatcher --verbose --sleep /path/to/your/sleepscript --wakeup /path/to/your/wakeupscript

A simple hello/goodbye script

Now that sleepwatcher is installed, let’s make the computer say Welcome back, <yourname> on wake and Goodbye on sleep. Two files are needed for this:

  • sleepscript – script that runs when the computer sleeps
  • wakescript – script that runs when the computer wakes

The scripts can be placed anywhere but the standard place would be to place them in /Users/<username>/bin. The bin folder should contain:

  • /Users/<username>/bin/sleepscript
  • /Users/<username>/bin/wakescript

The scripts should contain:

Sleep Script

function say_goodbye {
	say -v Vicki "goodbye"
}
say_goodbye

Wake Script

function say_hello {
	say -v Vicki "Welcome back, username"
}
say_hello

Make sure to chmod the scripts to 775. Next, launch the daemon to test:

$ /usr/local/sbin/sleepwatcher -d --sleep /Users/<username>/bin/sleepscript --wakeup /Users/<username>/bin/wakescript

Demo

Here is a video demo showing my Macbook Air saying “Welcome back” and “Goodbye”. 1337!

Happy Coding!

Import MySQL database with progress bar in shell script

I love discovering new stuff. I also love progress indicators.

Importing large databases into MySQL can be nerve racking without a progress bar. Here is a simple way to create a shell progress indicator using pv.

pv /path/to/sqlfile.sql | mysql -uUSERNAME -pPASSWORD -D DATABASE_NAME

If you are not on a server you may use a GTK version using zenity:

(pv -n /path/to/sqlfile.sql | mysql -uUSERNAME -pPASSWORD -D DATABASE_NAME) 2>&1 |  zenity --width 550 --progress --auto-close --auto-kill --title "Title of progress indicator"

The latter version came from commandlinefu.
You can make a progress bar for pretty much anything using pv.

Ubuntu and Mac do not come with pv. To install in:
Ubuntu

sudo apt-get install pv

Mac (using homebrew)

brew install pv

Custom Bash Shell for Development

Spending lots of time in the shell? Tired of the default shell? Hopefully this tutorial can be a time saver if you are a developer that likes a more informative shell with Git and virtualenv shortcuts. It’s based on Steve Losh’s Extravagant Zsh prompt and Armin Ronachers dotfiles (which is a port of the zsh prompt) but we are focusing on the latter.  Note: this was done in Ubuntu Lucid 10.01. It may vary depending on your OS.

You will need the following for this tutorial (download the files):

You should have a file list that looks something like this:

git-1.5.4.3.tar.bz2
mitsuhiko-dotfiles-3dea73c.tar.gz
standalone
vcprompt-871f3fbc9c69.gz
virtualenvwrapper-2.5.2.tar.gz

Extract the files above (I put them in ~/bash/). Make the directories (from home directory) that are needed for the bashrc script and supplemental files:

glenbot@ubuntu:~$ mkdir .virtualenvs
glenbot@ubuntu:~$ mkdir bin
glenbot@ubuntu:~$ sudo mkdir -p /usr/local/git/contrib/completion

Copy the git-completion.bash script to /usr/local/git/contrib/completion

sudo cp ~/bash/git-1.5.4.3/contrib/completion/git-completion.bash /usr/local/git/contrib/completion/git-completion.bash

Install virtualenvwrapper:

glenbot@ubuntu:~/bash/virtualenvwrapper-2.5.2$ sudo python setup.py install

Build and install vcprompt:

glenbot@ubuntu:~/bash/vcprompt$ make
cc -Wall -Wextra -Wno-unused-parameter -g -O2   -c -o src/common.o src/common.c
cc -Wall -Wextra -Wno-unused-parameter -g -O2   -c -o src/cvs.o src/cvs.c
cc -Wall -Wextra -Wno-unused-parameter -g -O2   -c -o src/git.o src/git.c
cc -Wall -Wextra -Wno-unused-parameter -g -O2   -c -o src/hg.o src/hg.c
cc -Wall -Wextra -Wno-unused-parameter -g -O2   -c -o src/svn.o src/svn.c
cc -Wall -Wextra -Wno-unused-parameter -g -O2   -c -o src/vcprompt.o src/vcprompt.c
cc -o vcprompt src/common.o src/cvs.o src/git.o src/hg.o src/svn.o src/vcprompt.o

glenbot@ubuntu:~/bash/vcprompt$ cp -p vcprompt ~/bin

Copy and rename hub standalone to your ~/bin directory:

glenbot@ubuntu:~$ cp -p ~/bash/standalone ~/bin/hub
glenbot@ubuntu:~$ chmod 775 ~/bin/hub

Make a backup of your .bashrc file and copy over mistuhiko’s bashrc file:

glenbot@ubuntu:~$ cp .bashrc bashrc-backup
glenbot@ubuntu:~$ cp ~/bash/mitsuhiko-dotfiles-3dea73c/bash/bashrc .bashrc

Make sure that MITSUHIKOS_VCPROMPT_EXECUTABLE=~/bin/vcprompt in the bashrc file

Logout and log back in and you should have a nice new terminal that looks like this:

Here is what it all means.

  • glenbot – username logged into the system
  • ubuntu – hostname of the system
  • ~/code/vidcast – directory you are in
  • git:master+ – current git branch. The (+) means modifications to tracked files. The (?) marks untracked files.

You can also use the git auto complete by typing git <tab><tab>. Make sure you have ruby installed for git auto-completion:

sudo apt-get install ruby

Now you can use virtualenvwrapper to contain all your python virtual environments in ~/.virtualenvs and by using the “workon” command you can switch between them. Here is an example creating a virtualenv called vidcast-env:

glenbot at ubuntu in ~
$ mkvirtualenv vidcast-env
New python executable in vidcast-env/bin/python
Installing distribute........................................................................done.
virtualenvwrapper.user_scripts Creating /home/glenbot/.virtualenvs/vidcast-env/bin/predeactivate
virtualenvwrapper.user_scripts Creating /home/glenbot/.virtualenvs/vidcast-env/bin/postdeactivate
virtualenvwrapper.user_scripts Creating /home/glenbot/.virtualenvs/vidcast-env/bin/preactivate
virtualenvwrapper.user_scripts Creating /home/glenbot/.virtualenvs/vidcast-env/bin/postactivate
virtualenvwrapper.user_scripts Creating /home/glenbot/.virtualenvs/vidcast-env/bin/get_env_details
(vidcast-env)
glenbot at ubuntu in ~
$ workon vidcast-env
(vidcast-env)
glenbot at ubuntu in ~
$

If you have any errors when running mkvirtualenv it may be an issue with your version of virtualenv. I had to uninstall it by running apt-get remove python-virtualenv and then downloading the lastest package of virtualenv.

Fork me on GitHub