Created
October 19, 2015 22:49
-
-
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.
This file contains 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 | |
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