Skip to content

Instantly share code, notes, and snippets.

@razius
Last active December 26, 2015 12:19
Show Gist options
  • Save razius/7150739 to your computer and use it in GitHub Desktop.
Save razius/7150739 to your computer and use it in GitHub Desktop.
Check if a command is running already, if not start it with the provided arguments.
#!/bin/bash
# Check if a command is running already, if not start it with the provided arguments.
#
# Example:
# This will check if thunar is already running if not it will start it with the --daemon argument.
# ./run-once.sh thunar --daemon
#
# I use it for my awesomewm autostart script which is available here:
# https://github.com/razius/awesomewm/blob/master/autostart.sh
PID=`pgrep -f -x "$1"`
if [[ -z $PID ]]
then
echo "Starting $@"
`$@ > /dev/null 2>&1 &`
else
echo "$1 already running with $PID."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment