ragingSloth
ragingSloth

Reputation: 1104

different behavior in Python shell and program

I'm using subprocess.Popen to instantiate an ssh-agent, add a key and push a git repository to a remote. To do this I string them together with &&. The code I'm using is

subprocess.Popen("eval $(ssh-agent) && ssh-add /root/.ssh/test_rsa && git push target HEAD", shell=True)

When I run this as a .py file I am prompted for the key's password. This seems to work as I get.

Identity added: /root/.ssh/test_rsa (/root/.ssh/test_rsa).

But when it tries to push the repository to the remote, an error occurs.

ssh: connect to host ***.***.***.*** port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

However, if I simply run the same command in the interactive shell, it works. What causes this difference in behaviour, and what can I do to fix this?

Upvotes: 1

Views: 133

Answers (1)

ragingSloth
ragingSloth

Reputation: 1104

The git server was on an aws instance that was being started earlier in the script. There was a check to make sure it was running, but aws seems to report an instance as running once boot has begun. This means that there is a brief time in which the instance is running, but an ssh daemon doesn't exist. Because the script moved very quickly into trying to push, it was falling within this time period and the server was refusing its connection attempt. By the time I would try anything in the interactive shell the instance was running long enough that it was working.

In short, aws says instances are running before the OS has started services.

Upvotes: 1

Related Questions