Skip to content

Instantly share code, notes, and snippets.

@connordavison
Created October 19, 2015 22:49
Show Gist options
  • Save connordavison/40d25ee5669fa7c9f3dc to your computer and use it in GitHub Desktop.
Save connordavison/40d25ee5669fa7c9f3dc to your computer and use it in GitHub Desktop.
Shell script to start & daemonise a process. Detaches the process from the console & redirects its output to /dev/null.
#!/bin/bash
function iscmd()
{
if hash $1 2>/dev/null; then
return 0
fi
return 1
}
iscmd $1
# Confirm command $1 exists
if [ $? -eq 0 ]; then
# Execute positional parameters ("$@" prevents parameters from
# breaking; contrast to $*).
"$@" &> /dev/null &
# Retain PID of the executed command. $! cannot be directly echoed
# since it would yield the PID of the echo process.
PID=$!
echo "PID:" $PID
# Successful exit
exit 0
fi
echo $1 "is not a command."
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment