SSH – How to Detach Remote Command with Pseudo Terminal

nohupsshterminal

i am running videos on raspberryPi+bigMonitor few meters from me with command:

ssh    pi 'omxplayer file.mp4 </dev/null &>/dev/null &'

that works great.

But if i want to use ssh's virtual terminal allocation -t that command do not works anymore.

I really need -t for using great ncurses based video menu with 'fzf' before omxplayer, but all of my specific problems are not important, because main problem can be simulated with sleep 10 instead of omxplayer:

on remote computer:

watch -n1 'ps aux | grep [s]leep\ 10'   #monitoring if 'sleep' running.

on local computer various tests:

ssh -t pi 'sleep 10 </dev/null &>/dev/null &'                  #not working
ssh -t pi 'nohup  sleep 10 </dev/null &>/dev/null & disown'    #not working
ssh -t pi 'setsid sleep 10 </dev/null &>/dev/null &'           #not working
ssh -t pi 'nohup  sleep 10 </dev/null &>/dev/null & sleep 0'   #works OK !!!!
ssh -t pi 'setsid sleep 10 </dev/null &>/dev/null & sleep 0'   #works OK
ssh -t pi 'nohup  sleep 10 </dev/null &>/dev/null & /bin/true' #not working
ssh -t pi 'nohup  sleep 10 </dev/null &>/dev/null & (true & wait)' #works ok

'sleep 0' in the end is probably the best solution, proposed by @egmont.

Still wondering if there is some better, more reliable solution, and what was exact cause for this (probably ssh closing vt is too fast for parallel nohup/setsid run).

Best Answer

The issue appears to be that the background shell that will turn into nohup to run sleep or omxplayer is getting a SIGHUP before it has turned into nohup and set the signal handler to ignore.

ssh -t pi 'trap HUP "" ; omxplayer file.mp4 </dev/null &>/dev/null &'

works for me every time, whilst some of the other approaches like sleep 0 sometimes fail.

Related Question