ftruncate Action

The ftruncate action truncates standard output file, but does not truncate standard error file. Perform the following steps to truncate a standard error file:

  1. Re-open stdout as stderr.

  2. Truncate stderr.

  3. Re-open the original stdout.

Following example displays how to truncate a standard error file.

#  cat > script.d
#!/usr/sbin/dtrace -s

#pragma D option quiet
#pragma D option destructive

BEGIN
{
	printf("data\n");
	freopen("/dev/stderr");
	ftruncate();
	freopen("");	/* re-open original stdout */
	printf("more data\n");
	exit(0);
}
# chmod a+x script.d
# echo foo > stdout
# echo foofoo > stderr
# ls -l std*
-rw-r--r--   1 root     root           7 Feb 27 14:21 stderr
-rw-r--r--   1 root     root           4 Feb 27 14:21 stdout
# ./script.d >> stdout 2 >> stderr
# ls -l std*
-rw-r--r--   1 root     root           0 Feb 27 14:26 stderr
-rw-r--r--   1 root     root          19 Feb 27 14:26 stdout
# cat stdout
foo
data
more data
#