The background shell script cannot reach directories after ssh exits, even with nohup

I want to run a shell script in the background on a server machine and run this shell script from an ssh connection. Despite the fact that I run a background process with the script nohup, background script fails due to an error that is not available for the catalog as soon as I close my ssh-connection (and not before).

runInBackground.sh

#!/bin/bash
...
nohup ./run.sh > /dev/null 2> local/errorLog.txt < /dev/null &

run.sh

#!/bin/bash
...
while [ true ] ; do
    ...
    cd optaplanner-examples
    mvn exec:exec // calls java process
    cd ..
done

So, when I start runInBackground.sh, everything works fine for several hours until I disconnect my ssh connection. As soon as I log out, it errorlog.txtpopulates:

java.io.FileNotFoundException: /home/myUser/server/optaplanner-simple-benchmark-daemon/local/output/
./run.sh: line 64: /home/myUser/server/optaplanner-simple-benchmark-daemon/local/processed/failed_machineReassignmentBenchmarkConfig.xml: No such file or directory
fatal: Could not change back to '(unreachable)/server/optaplanner-simple-benchmark-daemon/local/optaplannerGitClone/optaplanner': No such file or directory
ls: cannot access /home/myUser/server/optaplanner-simple-benchmark-daemon/local/input: No such file or directory
ls: cannot access /home/myUser/server/optaplanner-simple-benchmark-daemon/local/input: No such file or directory
ls: cannot access /home/myUser/server/optaplanner-simple-benchmark-daemon/local/input: No such file or directory
... // 1000+ more of that ls error

( Full source code )

+4
3

, , , , (, NFS ). . .

: , . , pty. , , SSH : ssh -T user@host ls /home/myUser/server. , .

, screen, SSH.

- SSH. . , utmp , , . , SSH. , systemd ssh :

[Unit]
Description=A tunnel to SOME_HOST
PartOf=sshd.service
Requires=network.service

[Service]
ExecStart=/usr/bin/ssh -N -q -R 2222:localhost:22 SOME_HOST
Restart=on-failure
RestartSec=5
User=tunnel
Group=tunnel

[Install]
WantedBy=sshd.service
WantedBy=network.service

, systemd , SSH .

+6

screen nohup.

ssh .

:

apt-get install screen (On Debian based Systems)

yum install screen (On RedHat based Systems)

( script stdout / stderr

cd your_app_directory_path
screen ./your_script.sh

( ), CTRL + A + D, .

, -

screen -r

screen -r <screen id or name>

, .

+1

screen, ssh . screen -r , .

0
source

Source: https://habr.com/ru/post/1620342/


All Articles