Created
May 30, 2019 05:13
-
-
Save joelnet/f2ffb0c25761ea0f10eb2ab2ddadfb7b to your computer and use it in GitHub Desktop.
Shell script to execute a command once (while running)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
pidfile=$1 | |
cmd=$2 | |
if [ -z "$pidfile" ] || [ -z "$cmd" ]; then | |
echo "Usage: once <pidfile> \"<command>\"" | |
exit 0 | |
fi | |
if [ -f "$pidfile" ] && kill -0 `cat $pidfile` 2>/dev/null; then | |
echo Still running [`cat $pidfile`] | |
exit 1 | |
fi | |
# echo $$ > $pidfile | |
$cmd& | |
echo Started [$!]: $cmd | |
echo $! > $pidfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment