Ssh – Execute command over SSH while logging live output

bashsshtee

I'm trying to log the output of a remote ssh command.

I'm current using

ssh USER@IP | tee -a ~/random.log

and then run the command I need to run on the remote server which then logs to random.log.

I need this to be able to run in the background, currently it exits when I quit/close terminal.

I cannot log on the remote server as it is a router with very little free space (>800kb).

Best Answer

You can use screen on the local machine as well, to prevent stopping the exit if you close your terminal window.

screen

and in the screen session,

ssh USER@IP | tee -a ~/random.log

If you accidentally close the window, just use screen -r to reattach. Note that this will not survive a reboot of your local machine.