Skip to content

Instantly share code, notes, and snippets.

@john-yuan
Created April 24, 2023 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-yuan/3ce15f9fb7b3fd3bfc8da821c023b4bc to your computer and use it in GitHub Desktop.
Save john-yuan/3ce15f9fb7b3fd3bfc8da821c023b4bc to your computer and use it in GitHub Desktop.
Linux shell script to get pid by process name.
#!/bin/bash
readonly CMD="${@:1}"
if [ "$CMD" == "" ]
then
echo "usage: $0 <command>"
exit 1
fi
ps --no-headers -ax -o "pid,command" | grep "$CMD" | grep -v "grep $CMD" | awk '{$1=$1};1' | cut -d ' ' -f 1 | grep -v $$
@john-yuan
Copy link
Author

Usage:

./getpid.sh my-web-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment