Skip to content

Instantly share code, notes, and snippets.

@ndgnuh
Created March 10, 2021 18:37
Show Gist options
  • Select an option

  • Save ndgnuh/59e1e35f79ec56307c5ff32ea7677b67 to your computer and use it in GitHub Desktop.

Select an option

Save ndgnuh/59e1e35f79ec56307c5ff32ea7677b67 to your computer and use it in GitHub Desktop.
Run once shell script
#!/bin/sh
BASEDIR="/var/run/user/$(id -u)/runonce"
COMMAND="$@"
HASH="$(echo "$COMMAND" | md5sum | cut -d ' ' -f 1)"
PIDFILE="$BASEDIR/$HASH"
show_help () {
echo "runonce <command>"
}
if [ -z $1 ]; then
show_help
exit
fi
mkdir -p $BASEDIR
if ! [ -f $PIDFILE ]; then
$COMMAND &
echo $! > $PIDFILE
wait
rm $PIDFILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment