On Tue, Dec 26, 2023 at 06:31:19PM +0000, Albretch Mueller wrote: > I think I am getting close to where I need to with this (am I?): > > $ bash -c 'ping www.google.fr -c 4 &'; echo "Caller PID: $$"; strace -p $$
I don't understand what you're trying to do here. Is this an oversimplified example that has been so twisted and distorted that we can't tell what the point is? If you want to strace a ping process, just use: strace ping www.google.fr -c 4 There is no reason to invoke a second shell, nor to run the ping process in the background, nor to use strace on an already-started ping process. Even if you DID for some reason want to launch a BACKGROUND PROCESS and the strace it, you're doing it wrong. The $$ you reference in your strace command is the PID of the script. Your bash -c is a child of the script, and your ping is a child of the bash -c (thus, a grandchild of the script). If you want to launch a SIMPLE BACKGROUND PROCESS (note the SIMPLE here, this is IMPORTANT), and then strace it while it runs, you'd do it like this: ping www.google.fr -c 4 & pid=$! strace -p "$pid" wait "$pid" Note that there is no "bash -c". This is IMPORTANT. The ping process is a direct child of the script. That's why we can get its PID with the $! special parameter, after launching it with & into the background. If the thing you're trying to strace is actually a COMPLEX DAEMON that does its own forking and shit, then this MAY NOT WORK. You could attempt to attach strace to the daemon's original PID, but if the daemon forks and then commits suicide (a VERY OUTDATED design), then your strace may be too late. It might be trying to attach to a process that's already dead. If the thing you're trying to strace is a long-running daemon that forks child process but DOES NOT kill itself, then it MIGHT work. You'd want the "-f" option to trace all child processes as well as the main PID. Now, you see, we cannot tell from your "ping" or "bash -c ping" example what it is that you're ACTUALLY trying to attach to. What is it REALLY? How does it behave? What information are you trying to capture in your strace? What PROBLEM are you even trying to solve? Why are you writing a SCRIPT to solve your problem instead of running an strace command by hand to get a one-off trace log so that you can analyze it? (Oh, also, just to add more bruises on the dead horse, ping is sometimes a setuid root program. Depends on the OS. If it's setuid root on your system, you might not be able to strace it without being root to start with.)