Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created June 20, 2012 00:36
Show Gist options
  • Save unixpickle/2957418 to your computer and use it in GitHub Desktop.
Save unixpickle/2957418 to your computer and use it in GitHub Desktop.
Get the process ID for a process name
#!/bin/bash
#
# Quickly get a PID for a given process name.
#
if [ $# -lt 1 ]; then
echo 'Usage: pidof <command name>' >&2
exit 1
fi
lines=`ps aux -c | grep "$1" | sed -e 's/^[^0-9]*\([0-9]*\).*/\1/g'`
if [ "$lines" = "" ]; then
echo "error: no process found: \`$1'" >&2
exit 1
fi
echo -n `echo ${lines} | head -n 1`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment