Python cron task – exit if already running

A simple way for Python cron tasks to exit if another process is currently running. Does not use a pidfile.

import os
import subprocess
import shlex

def bail_if_another_is_running():
    cmd = shlex.split("pgrep -u {} -f {}".format(os.getuid(), __file__))
    pids = subprocess.check_output(cmd).strip().split('\n')
    if len(pids) > 1:
        print "Exiting! Found {} is already running (pids): {}".format(
            __file__, " ".join(pids))
        raise SystemExit(1)

Virtualenvs with different interpreters

Update 2011-09-27: Turns out virtualenv and virtualenvwrapper support this out of the box. Most of what’s written below is horrifically complex compared to just using the -p switch when you make your virtualenv. You simply need to do this:

mkvirtualenv -p /path/to/some/python coolname

That’ll create a new virtualenv called “coolname” that uses /path/to/some/python for it’s Python interpreter. I’ve tested this with PyPy and it worked great.

SSH Agent Forwarding

This is part of the mini-series OpenSSH for Devs.

SSH agent forwarding let’s you lock down remote hosts while making them easier to access and use in automated ways. One co-worker succinctly describes agent forwarding as “the shit”.

SSH Config

This is part of the mini-series OpenSSH for Devs.

An SSH config let’s you set options you use often (e.g., the user to login as or the port to connect to) globally or per-host. It can save a lot of typing and helps make SSH Just Work.

OpenSSH for Devs

There have been many surprises as I’ve moved from Sysadmin to Coder. Some of them are a product of switching contexts: what was once “common knowledge” is now “tips & tricks” (and vice versa). One tool that has regularly come up is SSH. It can be painful to watch developers jump through unnecessary hoops (over and over again) in order to access remote hosts.