How to include nohup inside a bash script?

42,977

Solution 1

Create an alias of the same name in your bash (or preferred shell) startup file:

alias mandacalc="nohup mandacalc &"

Solution 2

Just put trap '' HUP on the beggining of your script.

Also if it creates child process someCommand& you will have to change them to nohup someCommand& to work properly... I have been researching this for a long time and only the combination of these two (the trap and nohup) works on my specific script where xterm closes too fast.

Solution 3

Why don't you just make a script containing nohup ./original_script ?

Solution 4

There is a nice answer here: http://compgroups.net/comp.unix.shell/can-a-script-nohup-itself/498135

#!/bin/bash

### make sure that the script is called with `nohup nice ...`
if [ "$1" != "calling_myself" ]
then
    # this script has *not* been called recursively by itself
    datestamp=$(date +%F | tr -d -)
    nohup_out=nohup-$datestamp.out
    nohup nice "$0" "calling_myself" "$@" > $nohup_out &
    sleep 1
    tail -f $nohup_out
    exit
else
    # this script has been called recursively by itself
    shift # remove the termination condition flag in $1
fi

### the rest of the script goes here
. . . . .
Share:
42,977
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a large script called mandacalc which I want to always run with the nohup command. If I call it from the command line as:

    nohup mandacalc &
    

    everything runs swiftly. But, if I try to include nohup inside my command, so I don't need to type it everytime I execute it, I get an error message.

    So far I tried these options:

    nohup (
    command1
    ....
    commandn
    exit 0
    )
    

    and also:

    nohup bash -c "
    command1
    ....
    commandn
    exit 0
    " # and also with single quotes.
    

    So far I only get error messages complaining about the implementation of the nohup command, or about other quotes used inside the script.

    cheers.