nohup does not work properly

nikosdi picture nikosdi · Nov 15, 2016 · Viewed 9.7k times · Source

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

Answer

Eric Duminil picture Eric Duminil · Nov 15, 2016

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