Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 20, 2020 std.process - avoid interaction with parent shell | ||||
---|---|---|---|---|
| ||||
I am doing some scripting via D, and using std.process.execute to git clone things. I don't want any user interaction. Occasionally, I get a repository that no longer exists (404). Then git comes up and asks for a username/password. I want it to just fail. Apparently git has no option to be non-interactive, it supposedly checks stdin to see if it's a tty, and only errors if it's not. I tried redirecting /dev/null to stdin when executing my application (and I assumed that would pass onto the process child), but it still asks. What am I doing wrong? e.g.: myapp < /dev/null Username for 'https://github.com': Program pauses, waits for my input. -Steve |
July 20, 2020 Re: std.process - avoid interaction with parent shell | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Mon, Jul 20, 2020 at 04:55:52PM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: > I am doing some scripting via D, and using std.process.execute to git clone things. > > I don't want any user interaction. Occasionally, I get a repository that no longer exists (404). Then git comes up and asks for a username/password. I want it to just fail. Apparently git has no option to be non-interactive, it supposedly checks stdin to see if it's a tty, and only errors if it's not. Try --no-pager perhaps? Not sure if that would help, since this isn't technically a pager that's prompting you. Another way is to take a look at std.process.execute's implementation. I believe it's just a wrapper around spawnProcess. What you want is to adapt that implementation so that it closes stdin before fork-n-exec'ing git; that should stop any prompts. One thing to be aware of is that it may not necessarily be git itself that's prompting you; it could be a helper program like a password manager that creates the prompt. In that case you probably have to find out what it is, and disable it somehow (usually by overriding some environment variable that gets passed to the git child process). T -- Ph.D. = Permanent head Damage |
July 20, 2020 Re: std.process - avoid interaction with parent shell | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On 7/20/20 5:24 PM, H. S. Teoh wrote: > On Mon, Jul 20, 2020 at 04:55:52PM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: >> I am doing some scripting via D, and using std.process.execute to git >> clone things. >> >> I don't want any user interaction. Occasionally, I get a repository >> that no longer exists (404). Then git comes up and asks for a >> username/password. I want it to just fail. Apparently git has no >> option to be non-interactive, it supposedly checks stdin to see if >> it's a tty, and only errors if it's not. > > Try --no-pager perhaps? Not sure if that would help, since this isn't > technically a pager that's prompting you. > > Another way is to take a look at std.process.execute's implementation. I > believe it's just a wrapper around spawnProcess. What you want is to > adapt that implementation so that it closes stdin before fork-n-exec'ing > git; that should stop any prompts. I ran the git command from the shell directly with < /dev/null and it still can ask for username/password. I don't know if it's possible to prevent it. > > One thing to be aware of is that it may not necessarily be git itself > that's prompting you; it could be a helper program like a password > manager that creates the prompt. In that case you probably have to find > out what it is, and disable it somehow (usually by overriding some > environment variable that gets passed to the git child process). I think you might be right. I don't know how it's accessing my terminal, but clearly it can keep doing so even without any handles open. I'm even using ctrl-D and it continues to come up with prompts and wait for input. I was able to solve it by backgrounding the process, and then quitting the parent shell, then it had no option but to error ;) I'm still interested in knowing how this works, if anyone knows. Searching for things like "how does git access my terminal when I closed stdin" doesn't give me much information. -Steve |
July 20, 2020 Re: std.process - avoid interaction with parent shell | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 20 July 2020 at 21:44:31 UTC, Steven Schveighoffer wrote:
>
> I think you might be right. I don't know how it's accessing my terminal, but clearly it can keep doing so even without any handles open.
Probably /dev/tty
|
July 20, 2020 Re: std.process - avoid interaction with parent shell | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 20 July 2020 at 20:55:52 UTC, Steven Schveighoffer wrote:
> I tried redirecting /dev/null to stdin when executing my application (and I assumed that would pass onto the process child), but it still asks. What am I doing wrong?
Generically, I think you want to detach the program from the current terminal (as well as doing the above). I think setsid can be used for this purpose.
Specifically, checking git's source code, I see that setting the environment variable GIT_TERMINAL_PROMPT to 0 will disable password prompts.
|
July 20, 2020 Re: std.process - avoid interaction with parent shell | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 20 July 2020 at 20:55:52 UTC, Steven Schveighoffer wrote:
>
> I don't want any user interaction. Occasionally, I get a repository that no longer exists (404). Then git comes up and asks for a username/password. I want it to just fail. Apparently git has no option to be non-interactive, it supposedly checks stdin to see if it's a tty, and only errors if it's not.
>
>
> -Steve
Try setting GIT_TERMINAL_PROMPT=0 as an environment variable.
|
July 21, 2020 Re: std.process - avoid interaction with parent shell | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | On 7/20/20 6:04 PM, Vladimir Panteleev wrote: > On Monday, 20 July 2020 at 20:55:52 UTC, Steven Schveighoffer wrote: >> I tried redirecting /dev/null to stdin when executing my application (and I assumed that would pass onto the process child), but it still asks. What am I doing wrong? > > Generically, I think you want to detach the program from the current terminal (as well as doing the above). I think setsid can be used for this purpose. Would be a good option possibly to put into std.process. > > Specifically, checking git's source code, I see that setting the environment variable GIT_TERMINAL_PROMPT to 0 will disable password prompts. > Thanks to you and FreeSlave, that seems to work. I had already solved it by just quitting the terminal with it still running. But good to know for the future! -Steve |
Copyright © 1999-2021 by the D Language Foundation