3

I am using PDSH to control multiple servers through one command line.

The command below works fine on individual servers, but when I enter the export command within PDSH, echoing it back is empty.

Any ideas on why this is, or how to fix it?

Specific example:

This works just fine:

# export CRAWL_DATE=$(date +%Y%m%d);
# echo $CRAWL_DATE
20131206

but in PDSH, it echos blank values:

pdsh> export CRAWL_DATE=$(date +%Y%m%d);
pdsh> echo $CRAWL_DATE;
<IP 1>: 
<IP 2>:
d-_-b
  • 1,167
  • 5
  • 18
  • 27

1 Answers1

4

When I run the following command it appears to work just fine.

$ pdsh -w root@skinner,root@mulder \
    'export CRAWL_DATE=$(date +%Y%m%d); echo $CRAWL_DATE;'
skinner: 20131206
mulder: 20131206

Running as you're describing shows this:

$ pdsh -w root@skinner,root@mulder
pdsh> export CRAWL_DATE=$(date +%Y%m%d);
pdsh> echo $CRAWL_DATE;
skinner: 
mulder: 

Running it this way works though:

pdsh> export CRAWL_DATE=$(date +%Y%m%d); echo $CRAWL_DATE;
skinner: 20131206
mulder: 20131206

So why didn't method #2 work?

The likely reason is that each time you run a command in pdsh and hit return that command is executed via ssh on each host. The next command that is executed is run in an entirely different ssh session with a different shell, so the variable doesn't exist.

This issue that was filed in 2011 I think gives credence to my conclusion. Issue 37: Support user defined variables in pdsh.

There was an interesting workaround mentioned in that issue, mainly:

$ pdsh -w ^hostfile -Rexec ssh -2 -l %u %h '. ~/.pdsh/vars/%h; command'

This method makes use of a file that would contain the variables, and each command could then source this file, . ~/.pdsh/vars/%h each time a command is run remotely.

slm
  • 363,520
  • 117
  • 767
  • 871
  • My guess would be that in method 2, it's running each command in a different shell. – jordanm Dec 07 '13 at 03:12
  • @jordanm - mine too. I'm looking into the setting of variables using pdsh now. That seems to be where the smoke is coming from. – slm Dec 07 '13 at 03:15
  • @jordanm - there is this feature request which makes me believe that the exporting of variables isn't supported, because each command is run as a `ssh` each time you hit return. – slm Dec 07 '13 at 03:17
  • exported environment variables are not persisted between sessions. In method2, each command is batched out in a fresh ssh session. – ryanj Dec 07 '13 at 03:36