8

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
nikosdi
  • 2,138
  • 5
  • 26
  • 35
  • Not sure, but suggestions: (1) Redirect standard error to the log file too; if feeling paranoid, redirect standard input from `/dev/null`; (2) consider adding signal handlers for HUP, INT, QUIT, TERM, PIPE and any other signals you think might be relevant (HUP and TERM are the most important in my view), and have each signal handler at least report which signal was received. Then rerun the script and disconnect and reconnect. You could also add timestamping to the log file, at least at the start of each iteration (and the signal handlers too), so that you know when things happen. – Jonathan Leffler Nov 15 '16 at 16:02
  • [How can I close a terminal without killing the command running in it?](http://unix.stackexchange.com/q/4004), [Keeping a process running after putty or terminal has been closed](http://unix.stackexchange.com/q/89483), [Avoid delays from network calls in logon and logoff scripts](http://unix.stackexchange.com/q/289221), etc. I found I needed to use `disown` for best results. Best results include where messages are spewed after the child is detached. – jww Nov 15 '16 at 16:04
  • You could use the `disown` command from your terminal to supposedly achieve the same thing -- but note that most systems have a facility to do exactly what you want: start and monitor a process, and restart it if it dies. Read up in `init scripts` and the like (the actual method varies by system). – gilez Nov 15 '16 at 20:46

2 Answers2

5

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

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 1
    Im not downvoting, but this is usage of external application. Im strugling with similar issue on router, where installing things is not possible, and enviroment is very minimalistic. – Gall Annonim Nov 12 '17 at 22:26
2

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

srp
  • 619
  • 7
  • 18