Amazon-ssm-agent: Piping commands to start-session causes infinite loop of shell prompts.

Created on 11 Jan 2019  路  7Comments  路  Source: aws/amazon-ssm-agent

I'm attempting to write a wrapper script to start an ssm session in a bash prompt using the following:

echo '/bin/bash' | aws ssm start-session --target $instance_id

When I do this, I get an infinite loop of bash prompts and must terminate the process externally. The session is also left open and must be terminated manually with aws ssm terminate-session Similarly, if I pipe something like echo hello, I see "hello" printed, but then get the infinite loop of shell prompts.

Only echo 'exit' | aws ssm start-session doesn't exhibit this behavior, instead exiting immediately.

Most helpful comment

@phene The pipe is closed while ssm is not so that's causing problems. Luckily this works: https://stackoverflow.com/a/5852578/2115135

(echo '/bin/bash' && cat) | aws ssm start-session --target $instance_id

All 7 comments

@phene The pipe is closed while ssm is not so that's causing problems. Luckily this works: https://stackoverflow.com/a/5852578/2115135

(echo '/bin/bash' && cat) | aws ssm start-session --target $instance_id

FYI, that works great, just don't ever try to exit the shell with ctrl+d, otherwise you end up with the infinite loop of prompts that I described before.

Exiting the shell without the command piping using ctrl+d works as normal.

@phene Yeah, that probably could be fixed by killing the aws cli on the client side after the cat command exits but I don't have a ready snippet for that one. Or you could try expect.

This seems to work in the case of ctrl-d:

(echo '/bin/bash' && cat && echo 'exit' && echo 'exit') | aws ssm start-session --target $instance_id

Both exits are needed (1 for /bin/bash, the other for /bin/sh). The problem is other control commands are totally broken. If I run something like top after starting the session, exiting with ctrl-c gives me the infinite prompt issue again. My guess is that cat is receiving and processing the ctrl-c rather than top.

Thank you for the feedback! We will look into this.

I was able to get the ctrl-c stuff to work along with other items by adding stty raw -echo ; prior to the (...) and then after the aws ssm start-session... I add ; reset to reset my tty. The only issue I am really running into is the cat command hanging and having to hit a key.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shockie picture shockie  路  3Comments

thedevopsmachine picture thedevopsmachine  路  4Comments

arnaudmm picture arnaudmm  路  4Comments

heydonovan picture heydonovan  路  3Comments

evanstoddard23 picture evanstoddard23  路  4Comments