Skip to content

Instantly share code, notes, and snippets.

@joelnet
Created May 30, 2019 05:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelnet/f2ffb0c25761ea0f10eb2ab2ddadfb7b to your computer and use it in GitHub Desktop.
Save joelnet/f2ffb0c25761ea0f10eb2ab2ddadfb7b to your computer and use it in GitHub Desktop.
Shell script to execute a command once (while running)
#!/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