-
Notifications
You must be signed in to change notification settings - Fork 34
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
Get the pid of a started command #48
Comments
There isn't, mainly because an expression can be a group of pids and not just one. In fact if you're using We could expose pids if the handle of a running expression had a structure that mirrored the expression itself, where you could go piece by piece and ask what each one's pid is. That's doable, but it's a pretty big increase from an api perspective. Out of curiosity, what are you thinking about doing with the pid? If you want to send signals, there's already support for that. |
I need to write the pid to a file. My app dies and leaves zombies running (on purpose, the app is meant to start a bunch of spring boot services and node servers). Currently I can accomplish this using
but I would really like to use your library because it's able to combine stdout and stderr, which from what I've seen is quite difficult (for me at least). |
Hmm, yeah. If all you want to do is the stdout+stderr pipe, it might be simpler to just call into use os_pipe::{pipe, IntoStdio, PipeReader};
use std::io;
use std::process::{Command, Child, Stdio};
// A key detail when juggling pipes by hand is that reading the output
// of a pipe will block until all the write ends are closed. That means
// we have to be careful to close them immediately after spawning a
// child, including the ends that are *inside* the Command object. That
// gives us two options:
// 1) Take the Command by value, so that it automatically drops at the
// end of the function. (Simple but maybe annoying for the caller.)
// 2) Take an &mut Command, but manually overwrite its stdout and stderr
// fields after spawning, to force the pipes there to drop.
fn spawn_joined_by_value(mut command: Command) -> io::Result<(Child, PipeReader)> {
let (reader, writer) = pipe()?;
let writer_clone = writer.try_clone()?;
command.stdout(writer.into_stdio());
command.stderr(writer_clone.into_stdio());
let child = command.spawn()?;
Ok((child, reader)) // command and its pipe writers automatically drop here
}
fn spawn_joined_by_reference(command: &mut Command) -> io::Result<(Child, PipeReader)> {
let (reader, writer) = pipe()?;
let writer_clone = writer.try_clone()?;
command.stdout(writer.into_stdio());
command.stderr(writer_clone.into_stdio());
let child = command.spawn()?;
// Force the pipe writers to drop before returning.
command.stdout(Stdio::inherit());
command.stderr(Stdio::inherit());
Ok((child, reader))
} |
Thinking out loud: |
Ok thank you for the help. I'll try out that code and see if I can get it working. I'm not truly trying to manage the child processes, I just want to start some children, have the app exit, and be able to find those children later, so I was using the pids for that. But if your library isn't aimed at that then I'll have to continue down the path I already was. Thank you very much! |
Let me know how it goes. Also a couple thoughts that you might already be familiar with:
|
Ok I'm back around to this after working on some other stuff. I knew your first point there, but not the second. Also, if I need to wait for some text to appear in the output, but I want to be logging everything to a file, how would I accomplish that? I have a few ideas, but I'm unsure if they're 'rusty' or if I'm even on the right track. |
Instead, you want to get your hands on the bytes that come out the other end, by reading from the |
Ah, I guess the problem I wanted to solve is how do I get the child to continue writing to the file after rust exits. So say I read from the file (concurrently writing the lines to a log), when I reach the lines that look like so
I want to stop reading from the file, and I want rust to be able to exit and keep writing to that log, like |
Ah yes, that's interesting. I don't think Linux provides a way for a pipe write to automatically end up in two files, or in a pipe and a file. I think at the end of the day some process has to sit in the middle and copy those bytes. On the command line, people often do this with
So you could spawn children with an extra |
Fantastic. This is the kind of lead I needed I think. I'll look into those things. Thank you very much! I thought about a |
Is there any way to retrieve the pid of a running command?
The text was updated successfully, but these errors were encountered: