skip to Main Content

I’ve got a script, that will use ssh to login to another machine and run a script there. My local script will redirect all the output to a file. It works fine in most cases, but on certain remote machines, i am capturing output that i don’t want, and it looks like it’s coming from stderr. Maybe because of the way bash is processing entries in its start-up files, but this is just speculation.

Here is an example of some unwanted lines that end up in my file.

which: no node in (/usr/local/bin:/bin:/usr/bin)

stty: standard input: Invalid argument

My current method is to just strip the predictable output that i don’t want, but it feels like bad practice.

How can i capture output from only my script?

Here’s the line that runs the remote script.

ssh -p 22 -tq user@centos-server "/path/to/script.sh" > capture

The ssh uses authorized_keys.

Edit: In the meantime, i’m going to work on directing the output from my script on machine B to a file and then copying it to A via scp and deleting it on B. But i would really like to be able to suppress the output completely, because when i run the script on machine A, it makes the output difficult to read.

2

Answers


  1. YOu can redirect stdout (2) to /dev/null and redirect the rest to the log fole as follows:

    ssh -p 22 -tq user@centos-server bash -c "/path/to/script.sh" 2>/dev/null >> capture
     
    
    Login or Signup to reply.
  2. To build on your comment on Raman’s answer. Have you tried supressing .bashrc and .bash_profile as shown below?

    ssh -p 22 -tq user@centos-server "bash --norc --noprofile /path/to/script.sh" > capture

    If rc-files is the problem on some servers you should try and fix the broken rc-files instead of your script/invocation since it’ll affect all (non-interactive) logins.

    Try running ssh user@host 'grep -ls "which node" .*' on all your servers to find if they have "which node" in any dotfiles as indicated by your error message.

    Another thing to look out for is your shebang. You tag this as bash and write CentOS but on a Debian/Ubuntu server #!/bin/sh gives you dash instead of (sh-compatible) bash.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search