So combining the answer from this question with some hackidy hack nonsense...
Really all you need to do is this:
(( python3 my_script.py 0<&- 2>&1 | tee -a ${OUTFILE} | nc -kl ${PORT} &) &)
Explanation:
- Run your python script:
python3 my_script.py
- Detach stdin (so it can run independently):
... 0<&-
- Redirect stderr to stdout so we can pipe them along together:
... 2>&1
- Append output to some file but also keep the output going to stdout so we can pipe it someplace else:
... | tee -a ${OUTFILE} |
- Pipe stdout to a netcat listening port so this machine can essentially "serve" the stdout from your python script:
... | nc -kl ${PORT}
- Create a "double nested background subshell" this is explained in the link above but this will allow you to orphan "blah" so that it'll run even if your ssh connection ends
(( ... blah ... &) &)
To view the stdout/stderr of my_script.py
you now have several options. If you are still logged into that remote machine you can:
tail -f ${OUTFILE}
# Same "OUTFILE" used in explanation component 4
nc localhost $PORT
# Same "PORT" used in explanation component 5
If you are no longer logged in and you are now on a different machine (but on the same network) you can:
nc ${remote_machine} $PORT
# Same "PORT" used in explanation component 5
Where ${remote_machine}
is the hostname or IP address of the machine you ssh'ed into to run your command
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…