nikosdi
nikosdi

Reputation: 2158

nohup does not work properly

I have a very simple bash script. I want this bash script to continue running after I log out from the terminal since it is monitoring some services. However, I have the problem that as soon as I exit the terminal the process is terminated.

I run the process as:

nohup ./test.sh > test.log & 

I check the process using:

 ps -aux | grep test.sh

When I run the process I check that the script is running. However, when I reconnect the script is not running anymore.

The content of the test.sh file is the following:

#!/bin/bash
while :
do
        echo `date`": ****** Scheduled Test *****"
        result1=$(wget "127.0.0.1" -q -O -)
        result2=$(wget "127.0.0.1" -q -O -)
        echo ": $result1"
        echo ": $result2"
        if [[ $result1 == *"Running OK"* ]] && [[ $result2 == *"Running OK"* ]];
        then
                echo "***** Running OK ***** :)"
                sleep 60
                continue
        fi
        echo "@@@@@ Not Running @@@@@"
        echo "-----> Killing JARS"
        kill -9 `lsof -i:4445 | cut -d' ' -f5`
        kill -9 `lsof -i:4423 | cut -d' ' -f5`
        ./runsomejar.sh > jar1.log & 
        echo "-----> Restarting jar1"
        sleep 60
        echo "-----> Restarting jar2"
        ./runsomejar.sh > jar2.log &
        sleep 180
done

Upvotes: 9

Views: 25166

Answers (2)

srp
srp

Reputation: 753

I think you might have already figured out the root cause and a working solution.

But I am writing this answer so that it will be helpful to someone else.

This might be the root cause.

Still there is no guarantee in consistency of nohup (mentioned at the end of given answer).

Other commands to do the same thing.

  1. spawning subshells with parenthesis.

    (./test.sh > test.log &)

  2. disown in conjunction with the backgrounding ampersand.

    ./test.sh > test.log & disown

Upvotes: 3

Eric Duminil
Eric Duminil

Reputation: 54223

You could use screen :

screen -L ./test.sh

You can use Ctrl-A and d to detach the screen.

You can log out without killing test.sh

You can log back, and type

screen -r

to come back to your script.

Stdout will be logged to screenlog.0

Upvotes: 5

Related Questions