Skip to content

Instantly share code, notes, and snippets.

@adnan360
Created January 6, 2019 09:29
Show Gist options
  • Select an option

  • Save adnan360/8b223bd54a01dc7a20110808aef003b3 to your computer and use it in GitHub Desktop.

Select an option

Save adnan360/8b223bd54a01dc7a20110808aef003b3 to your computer and use it in GitHub Desktop.
Toggles a python script running (terminates if already running, runs otherwise)
#!/bin/bash
# This bash script (1) runs a python script if not already running
# or (2) kills the script if already running. Easily put, it inverts
# the running state of the script. Great for toggling GUI scripts.
# Run chmod +x python-invert.sh and then run this with the python
# script path as parameter: ./python-invert.sh path/to/script.py
script_name=${1}
if pgrep -f "python $script_name" &>/dev/null; then
pkill -f $script_name
else
python $script_name
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment