On 04/10/2017 05:56 AM, bruce wrote:
So if I get what you guys have said...
ssh crawl_user@1.2.3.4 2>&1 pgrep -f 'master_client' | wc -l
would work with the >> 2>&1 << writing the stderr to the stdout for the ssh
For curiosity's sake, let's look at how the shell parses and executes
that command line (roughly. I'm going to simplify a couple of things).
First, the command is parsed into tokens:
* ssh
* crawl_user@1.2.3.4
* 2>&1
* pgrep
* -f
* master_client
* |
* wc
* -l
Next, the tokens are divided into commands where control operators
appear. In this case, there is one control operator: the pipe
character. We now have two sets of tokens describing two commands:
[ssh, crawl_user@1.2.3.4, 2>&1, pgrep, -f, master_client] and [wc, -l].
Output redirection appears in the ssh command token set, and the shell
will use that to set up that command's output. It will not be passed as
an argument to ssh.
The shell then forks once for the ssh command, and again for the wc
command. The bash process which will execute ssh duplicates its stdout
FD for stderr. Its stdout FD is connected to the stdin FD of the bash
process that will execute wc. The second process's stderr and stdout
are both connected to the terminal that executed the command.
Once file descriptors have been arranged, each child process will exec()
their command. One will execute /usr/bin/ssh with arguments [ssh,
crawl_user@1.2.3.4, pgrep, -f, master_client]. The other will execute
/usr/bin/wc with arguments [wc, -l].
and at the same time the
>> pgrep -f 'master_client' | wc -l <<
is being run on the remote box..
wc will be executed locally because the pipe character is a control
operator, and it isn't quoted. It specifies the end of a command, and
also indicates what to do with that command's stdout.
or is it that the
>> pgrep -f 'master_client' <<
is run on the remote box.. with the
>> wc -l <<
then being run on the local box..
Yes, only pgrep will be run on the remote host. It is passed to
/usr/bin/ssh as an argument.
by the way, when running
ssh crawl_user@1.2.3.4 2>&1 "pgrep -f 'master_client' | wc -l"
is the pgrep inside the "" being all run on the remote?
Yes. It is passed as an argument to /usr/bin/ssh, which executes that
command on the remote host.
and if it is,
any idea why the count is 1 more than when i do a ssh into the box and
then run the
pgrep -f 'master_client' | wc -l <<
I'm not sure on that point. I'm not able to replicate any similar
discrepancy.
_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx