0

I would like to execute a long-running command via SSH/screen and have the output redirected to a file. I'm able to run the command fine (similar to this question), but the output never shows up.

Here is my current command:

ssh user@host screen -dm "ping -c 20 -i 5 localhost > /tmp/ping.out"

On the remote host I can see the session:

> screen -ls
There is a screen on:
        4530..com001     (Detached)
1 Socket in /var/run/screen/S-root.

And attach to view the live output, which terminates as expected with the ping command completes:

PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.060 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.071 ms
..

Everything does just what I need except the file redirect: ssh on the local host immediately disconnects, the command is running live in a screen session on the remote host (I can connect to the screen session and see my ping running), and the screen session terminates as soon as the ping command completes. The /tmp/ping.out file even gets created on the remote host! But the display stays in the screen session rather than getting redirected to the file.

What am I missing here?

(BTW, the ping is just for testing. Eventually I would like to park a 1 - 2 minute tcpdump and collect the output in the background for troubleshooting).

  • As a follow up, I've tried the tee command as well as various forms of redirect like "> /tmp/ping.log 2>&1". I've also experimented with the redirect inside and outside the quotes. – havoc1 Sep 22 '14 at 21:00
  • Also worth noting that I get the same results if I remove SSH from the equation and try the 'screen -dm' locally. eg. "screen -dm ping -c 10 -i 5 localhost > /tmp/ping.out" shows the same behavior as if I spawned it from the remote SSH command. – havoc1 Sep 22 '14 at 21:02
  • See my answer ; Screen output isn't what you are expecting. Start a shell inside screen and it will work. – CloudWeavers Sep 22 '14 at 21:03
2

Screen 'output' isn't exactly what you expect as it is designed as a multiplexor. Command you are looking for is:

ssh user@host screen -d -m "sh -c 'ping -c 20 -i 5 localhost > /tmp/ping.out'"

This command will spawn a new shell & work as intended.

  • That did the trick. The beauty of it is that the sh is still non-interactive, so the whole "screen->sh->ping" ball terminates when the ping command completes. – havoc1 Sep 22 '14 at 21:53
  • It's also worth noting that the '-L' option to enable output_logging in screen works when running the screen command locally (eg. "screen -dmL ping -c 20 -i 5 localhost") and sends output to /tmp/screen.0. However, the '-L' has no effect when passed through the ssh command from remote. – havoc1 Sep 22 '14 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.