Score:0

Don't kill remote script when local caller process gets killed

mg flag

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.

kanehekili avatar
zw flag
Im using either [tmux](https://man7.org/linux/man-pages/man1/tmux.1.html) or systemctl. The latter needs a small servicefile [example](https://askubuntu.com/a/1336578/906933) to be written.
Score:0
mg flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.