Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although you are sending the <code>ssh</code> process to the background via <code>&amp;</code>, its file descriptors are still "attached" to your terminal. A demonstration:</p> <pre><code># Terminal 1 $ ssh $host sleep 600 &amp; [3] 14340 # Terminal 2 $ ls -l /proc/14340/fd lrwx------ 1 iscfrc iscfrc 64 Oct 3 10:30 0 -&gt; /dev/pts/1 lrwx------ 1 iscfrc iscfrc 64 Oct 3 10:30 1 -&gt; /dev/pts/1 lrwx------ 1 iscfrc iscfrc 64 Oct 3 10:30 2 -&gt; /dev/pts/1 lr-x------ 1 iscfrc iscfrc 64 Oct 3 10:30 3 -&gt; socket:[125941180] lrwx------ 1 iscfrc iscfrc 64 Oct 3 10:31 4 -&gt; /dev/pts/1 lrwx------ 1 iscfrc iscfrc 64 Oct 3 10:31 5 -&gt; /dev/pts/1 lrwx------ 1 iscfrc iscfrc 64 Oct 3 10:31 6 -&gt; /dev/pts/1 </code></pre> <p>Note how all the FDs (Minus the TCP socket) are attached to <code>/dev/pts/1</code>.</p> <p>So what happens when your script runs <code>ssh</code>, puts it in to the background, but the <code>ssh</code> process continues to poll the terminal for input?</p> <pre><code># Terminal 2 $ sudo strace -p 14340 Process 14340 attached - interrupt to quit select(7, [3 4], [], NULL, NULL # Terminal 1 $ read # Terminal 2, output select(7, [3 4], [], NULL, NULL) = 1 (in [4]) read(4, 0xbff1dbbc, 16384) = ? ERESTARTSYS (To be restarted) --- SIGTTIN (Stopped (tty input)) @ 0 (0) --- --- SIGTTIN (Stopped (tty input)) @ 0 (0) --- # (infinite loop of the previous three lines) </code></pre> <p>It goes in to panic when attempting to <code>read()</code> input from file descriptor <code>4</code>, which you can see above is the terminal. (<code>4 -&gt; /dev/pts/1</code>)</p> <p>The solution is to disconnect <code>ssh</code> from the terminal's input; I recommend using <code>nohup</code>:</p> <pre><code>nohup ssh -q sc-2 tail -f /home/logs/oam-2.log &gt; /root/logs/file.log &amp; </code></pre> <p>Additional suggestions for consideration:</p> <ol> <li>Add the <code>-T</code> argument to your <code>ssh</code> command to disable TTY allocation on the remote host. Since you don't need to interact with the <code>tail</code> command a TTY is unnecessary.</li> <li>Store the PID of the <code>ssh</code> command in to a variable via <code>$!</code> in order to save yourself needing to parse the output of <code>ps</code>.</li> <li>And to be even more spartan, you can save yourself an <code>echo</code> by using <code>read</code>'s <code>-p</code> option. (<code>p</code> is for <em>prompt</em>)</li> </ol> <p>Combining all of the above:</p> <pre><code>nohup ssh -qT sc-2 tail -f /home/logs/oam-2.log &gt;/root/logs/file.log &amp; sshpid=$! read -p $'press return to stop the trace\n' kill $sshpid </code></pre> <p>I tested the above (With my own host/files paths filled in) and it ran for >10 minutes without stopping.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload