-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cron atty::is(atty::Stream::Stdin) #52
Comments
What cron-daemon are you using? (And what version?) |
I am using the cron that comes by default in the latest FreeBSD 13 (amd64): https://www.freebsd.org/cgi/man.cgi?cron(8) |
First of all: Assuming you do want to know whether the process was spawned from a terminal and not by your cron: isn't your logic backwards? let input_stdin = !atty::is(atty::Stream::Stdin); // isatty returns false if there's something in stdin.
if input_stdin {
println!("stdin"); // how to prevent this to happen when calling it from a cron
}
|
It has been working "fine" (https://github.com/s3m/s3m/blob/master/src/s3m/options.rs#L211) but I never tested within a
I have been using it to detect if something is in the stdin so that I can read the input and stream it, but maybe is not the way I should be doing this. |
Yes, that's indeed not how you do this. Tools typically just read until the "end of file" in such situations. |
It's not quite that simple. It's pretty typical for tools to change their behavior based on whether they believe stdin is readable or not. Whether stdin is attached to a tty or not is just one part of that calculation. Here's the logic that ripgrep uses as an example: https://docs.rs/grep-cli/0.1.6/src/grep_cli/lib.rs.html#190-213 Logically speaking: read from stdin if it's believed to be readable, where "believed to be readable" means "it's not a tty and has a file type amenable for reading." |
I will give it a try, thanks for sharing 👍 |
From a cron, I still getting like if there is something to read from wondering what could be the cons of using something like this:
|
You don't use a let mut line = String::new();
if std::io::stdin().read_line(&mut line).unwrap() > 0 { // read_line will return Ok(0) at EOF
// use line
} Of course, you'll probably want to handle read errors properly instead of just calling If your input is line-oriented, you can just use the iterator returned by Still, using the heuristics like the ones linked to by @BurntSushi before the first read does make sense if you want your program to be robust. These heuristics aim at ensuring stdin is "what you expect" and not something "weird" which may exhibit some unexpected behavior. If the check fails, you probably don't want to read from stdin at all but just pretend that it's already at EOF. Though I have to admit I myself usually don't include such checks in my programs. Or at least I didn't do so in the past. |
The problem I am trying to solve is to prevent reading from my app is a simple s3 uploader that can read from stdin (stream) or just pass the file from the command line (multipart upload):
or pipe something
But I notice what when running the app from a cron Because of this (since I may have some input) if the |
The problem is that your cron implementation is attaching a tty to stdin for some reason, based on what you told us. There is really nothing more to be done here because that is the problem. It's not a problem with this crate. You're only choice from what I can tell is to either add a flag that forcefully prevents reading from stdin, or somehow modify your heuristic for detecting whether stdin might be readable or not. You haven't really given us much to go on here, other than the fact that your cron daemon is advertising a tty on stdin for subprocesses it executes. (I find that highly unusual. You might consider looking into that issue more deeply, and possibly asking the maintainers why it's happening. Just make sure to provide a minimal reproducible example using libc apis directly.) Otherwise, you could probe at the stdin file descriptor a bit more. For example, what is its file type? |
I will search more about it, how could I test the file type? |
https://doc.rust-lang.org/stable/std/fs/struct.FileType.html Note also the FileTypeExt trait on Unix. |
I have a program that reads from
stdin
only when isatty returns falseWhen running this from a cron it always returns
true
like if there were something in stdin but there is nothing.Any idea how to prevent this so that I could call my app from a
cron
job and distinguish when there is something in stdin and not?The text was updated successfully, but these errors were encountered: