Bash set env variable is not working in pdsh

bashenvironment-variablespdsh

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>:

Best Answer

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.

Related Question