18

I have a cronjob that runs some tasks, and at the beginning of the script it runs is an ssh-add call ( it doesn't run as the user with the appropriate key so it needs adding, and in future it may well use a deploy key defined in version control ). This looks something like this:

ssh-agent bash -c "ssh-add /home/tomjn/.ssh/id_rsa; etc... "

I want to silence it, as this cronjob runs regularly, and I have a lot of emails in my inbox that are completely useless to me stating that yes, the key was added. I only want emails when things go wrong, like a git pull remote connection hang up etc, which does happen.

So TLDR, I keep seeing this:

Identity added: /home/tomjn/.ssh/id_rsa (/home/tomjn/.ssh/id_rsa)

How do I shut it up?

I've tried things like:

ssh-add /home/tomjn/.ssh/id_rsa > /dev/null

But to no avail. The man page doesn't indicate that there's a --quiet parameter, is there something else I can do to silence the output?

Tom J Nowell
  • 487
  • 1
  • 8
  • 17

1 Answers1

29

The output of the ssh-add command is on STDERR. You need to redirect STDERR to /dev/null to suppress the output:

mtak@frisbee:~$ ssh-add .ssh/id_rsa 2>/dev/null
mtak@frisbee:~$ 

or alternatively suppress all output by ending the command with >/dev/null 2>&1

mtak
  • 16,513
  • 2
  • 52
  • 64
  • 2
    A quiet flag would be finer-grained as it would allow to silence normal operation while allowing errors (like key not found, or not readable) to be spotted. A quick look at https://github.com/openssh/openssh-portable/blob/master/ssh-add.c confirms that there's no option in source code. Other options are to specifically filter the "added" message, but in bash this typically interferes with error handling (if you care to `set -eu` for some run-time sanity checks). No hint that all this is important here, so just redirecting stderr should be enough. – Stéphane Gourichon Jul 09 '16 at 14:48
  • 6
    There is now an option for this on [the man page](https://man.openbsd.org/ssh-add) Also, the code you linked does now have [a mention of this flag in the help output](https://github.com/openssh/openssh-portable/blob/master/ssh-add.c#L464) The flag was added in [this commit](https://github.com/openssh/openssh-portable/commit/530591a5795a02d01c78877d58604723918aac87) However, I have `OpenSSH_7.6p1` on my mac from homebrew (most recent available) and this flag doesnt seem to work - the `-q` option is available in the help, but it doesnt change key add output. – austinheiman Feb 15 '18 at 16:49
  • 1
    `-q` does not work on Kubuntu 18.04 either. – Geoffrey Jun 19 '19 at 08:19