I have a running cron job that will be going for a while and I'd like to view its stdout. I don't know how important the fact that the process was started by cron is, but I figure I'd mention it. This is on OSX so, I don't have access to things like... /proc/[pid]/..., or truss, or strace. Suggestions of executing with IO redirection (e.g. script > output & tail -f output
) are NOT acceptable, because this process is 1) already running, and 2) can't be stopped/restarted with redirection. If there are general solutions that will work across various Unices, that'd be ideal, but specifically I'm trying to accomplish this on a Mac right now.

- 4,283
- 3
- 33
- 41
-
Have you tried to attach the process to gdb and redirect the stdout? Here are the related posts: http://stackoverflow.com/questions/249703/how-can-a-process-intercept-stdout-and-stderr-of-another-process-on-linux and http://stackoverflow.com/questions/2874613/gdbosx-redirecting-stdout-may-cause-printf-to-have-a-214-bytes-buffer. Unfortunately, after playing with it a bit, I didn't have success with redirecting the stdout myself. But I'm not very fluent in gdb. – William Niu Aug 21 '10 at 04:39
5 Answers
True solution for OSX
Write the following function to your ~/.bashrc
or ~/.zshrc
.
capture() {
sudo dtrace -p "$1" -qn '
syscall::write*:entry
/pid == $target && arg0 == 1/ {
printf("%s", copyinstr(arg1, arg2));
}
'
}
Usage:
example@localhost:~$ perl -e 'STDOUT->autoflush; while (1) { print "Hello\n"; sleep 1; }' >/dev/null &
[1] 97755
example@localhost:~$ capture 97755
Hello
Hello
Hello
Hello
...
https://github.com/mivok/squirrelpouch/wiki/dtrace
NOTE:
You must disable dtrace
restriction on El Capitan or later.
csrutil enable --without dtrace

- 5,526
- 4
- 30
- 36
-
Thank you. 5+ years later, but this does in fact work. It's really a shame that Mark Renouf's answer has been so highly upvoted. While it may work on *nix systems, that was entirely not the point of the question. – theraccoonbear Nov 21 '15 at 18:21
-
3
-
-
9On MacOS Sierra (10.12.6 ) I get this error: `csrutil: requesting an unsupported configuration. This is likely to break in the future and leave your machine in an unknown state. csrutil: failed to modify system integrity configuration. This tool needs to be executed from the Recovery OS.` – Stan James Nov 24 '17 at 14:03
-
-
2It's an informative error, you have to do that prior to expecting this functionality to work. It holds true on 10.13 as well. – uchuugaka Apr 10 '18 at 02:37
-
-
2This post does not work for macOS 12.0.1 ---> dtrace: failed to grab pid 7696: (null), and https://github.com/mivok/squirrelpouch/wiki/dtrace does not exist. – SHI Zhong Ping Apr 22 '22 at 08:20
-
I get `dtrace: invalid probe specifier`, followed by `: probe description syscall::write*:entry does not match any probes. System Integrity Protection is on` – payne Nov 03 '22 at 04:46
-
Could you please explain the details of the code? I need stderr, not stdout, for instance. I'd also like to know if it completely redirects the pipe or if reads from it. This is relevant, because if it highjacks it, then using it on a command like `md5sum * > files.md5` would destroy the output (I realize you'd just tail the files.md5 file instead, but it's just to illustrate). – DanielSmedegaardBuus Apr 14 '23 at 06:04
-
After a bit of testing, I can see that this function does *not* redirect or block the original pipes of the process, so you won't mess with anything. It copies the process' output written to both STDOUT and STDERR, and it echoes both to local STDOUT. In my case, I explicitly need STDERR *without* STDOUT, as I forgot to pipe STDERR to a file in a long-running screen session, and I need to keep track of errors. Is that possible? – DanielSmedegaardBuus Apr 14 '23 at 06:23
DISCLAIMER: No clue if Mac has this. This technique exists on Linux. YMMV.
You can grab stdout/err from /proc (assuming proper privileges):
PID=$(pidof my_process)
tail -f /proc/$PID/fd/1
Or grab everything remaining in the buffer to a file:
cat /proc/$PID/fd/1
PS: fd/1 is stdout, fd/2 is stderr.
EDIT: Alex brown> Mac does not have this, but it's a useful tip for Linux.

- 41,819
- 10
- 94
- 108

- 30,697
- 19
- 94
- 123
-
4from my question: This is on OSX so, I don't have access to things like... /proc/[pid]/ – theraccoonbear Apr 22 '11 at 18:25
-
5
neercs has the ability to "grab" programs that were started outside it. Perhaps it will work for you. BTW, you don't have truss or strace, but you do have dtrace.

- 12,624
- 5
- 39
- 38
-
I got libcaca installed and was able to get neercs to find it, but neercs' ./configure is failing, starting with: lock.c:26:29: error: pam/pam_appl.h: No such file or directory, and then reports a number of other PAM related errors. – theraccoonbear Apr 22 '11 at 18:59
I think the fact you started with cron could save you. Under linux any standard output of a cron job is mailed to the unix mail account of the user who owns the job. Not sure about OSX though. Unfortunately you will have to wait for the the job to finish before the mail is sent and you can view the output.

- 8,428
- 2
- 31
- 43
-
unfortunately this is (was) a long running process and I wanted to get the output before its lengthy run time was complete. – theraccoonbear Apr 10 '11 at 04:33