How to include nohup inside a bash script?

Try putting this at the beginning of your script:

#!/bin/bash

case "$1" in
    -d|--daemon)
        $0 < /dev/null &> /dev/null & disown
        exit 0
        ;;
    *)
        ;;
esac

# do stuff here

If you now start your script with --daemon as an argument, it will restart itself detached from your current shell.

You can still run your script “in the foreground” by starting it without this option.

Leave a Comment