0

Problem

I call a long running process via ssh. And it's necessary that this process will not be killed. The user may kill the initial process on its host and I cannot do anything about it.

How to make the remote process not be killed if the initial process gets killed?

User -> user.sh -> SSH-to-remote -> long-running.sh

It must:

  1. Output the output of the remoteprocess
  2. Exit when the remote process exits
  3. Don't kill the remote process, wenn the initial process gets killed
  4. Make an output log file available

First incomplete solution

I thought about something using nohup but it didn't work so far:

User -> user.sh -> SSH-to-remote -> init.sh -> long-running.sh

init.sh

#!/bin/bash
nohup ./long-running.sh >out.log 2>&1 </dev/null &
pid=$!
disown $pid
tail -f out.log &
wait $pid

long-running.sh (content for testing)

#!/bin/bash
for i in $(seq 30)
do 
    echo $i
    sleep 1
done

This solution doesn't solve 2. It doesn't exit when the remote process exits.

1
  • Im using either tmux or systemctl. The latter needs a small servicefile example to be written.
    – kanehekili
    Dec 13, 2021 at 11:32

1 Answer 1

0

Looks like I found a solution.

init.sh

#!/bin/bash
nohup ./long-running.sh >out.log 2>&1 </dev/null &
pid=$!
disown $pid
tail --pid=$pid -f out.log

In my first solution wait didn't wait as disown removed the jobspec. tail ran forever and kept the session open.

Now wait was removed and tail waits for the process to exit. A kill from the source host kills tail but doesn't kills the long-running process.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .