system Action

void system(string program, ...)

The system action causes the program specified by program to be executed as if it were given to the shell as input. The program string can contain any of the printf or printa format conversions. Arguments must be specified that match the format conversions. For more information about valid format conversions, see Output Formatting in DTrace.

The following example runs the date command once per second:

# dtrace -wqn tick-1sec'{system("date")}'
 Tue Jul 20 11:56:26 CDT 2004
 Tue Jul 20 11:56:27 CDT 2004
 Tue Jul 20 11:56:28 CDT 2004
 Tue Jul 20 11:56:29 CDT 2004
 Tue Jul 20 11:56:30 CDT 2004

The following example shows a more elaborate use of the action, using printf conversions in the program string along with traditional filtering tools like pipes.

#pragma D option destructive
#pragma D option quiet

proc:::signal-send
/args[2] == SIGINT/
{
        printf("SIGINT sent to %s by ", args[1]->pr_fname);
        system("getent passwd %d | cut -d: -f5", uid);
}

The preceding script results in the following output.

# ./whosend.d
SIGINT sent to MozillaFirebird- by Jane Doe
SIGINT sent to run-mozilla.sh by Jane Doe
^C
SIGINT sent to dtrace by Jane Doe

The execution of the specified command does not occur in the context of the firing probe. It occurs when the buffer containing the details of the system action are processed at user-level. How and when this processing occurs depends on the buffering policy. For more information, see DTrace Buffers and Buffering. With the default buffering policy, the buffer processing rate is specified by the switchrate option. You can see the delay inherent in system if you explicitly tune the switchrate higher than its one-second default, as shown in the following example:

#pragma D option quiet
#pragma D option destructive
#pragma D option switchrate=5sec

tick-1sec
/n++ < 5/
{
        printf("walltime  : %Y\n", walltimestamp);
        printf("date      : ");
        system("date");
        printf("\n");
}

tick-1sec
/n == 5/
{
        exit(0);
}

Running the preceding script results in the following output.

# dtrace -s ./time.d
 walltime  : 2004 Jul 20 13:26:30
date      : Tue Jul 20 13:26:35 CDT 2004

walltime  : 2004 Jul 20 13:26:31
date      : Tue Jul 20 13:26:35 CDT 2004

walltime  : 2004 Jul 20 13:26:32
date      : Tue Jul 20 13:26:35 CDT 2004

walltime  : 2004 Jul 20 13:26:33
date      : Tue Jul 20 13:26:35 CDT 2004

walltime  : 2004 Jul 20 13:26:34
date      : Tue Jul 20 13:26:35 CDT 2004

Notice that the walltime values differ, but the date values are identical. This result reflects the fact that the execution of the date command occurred only when the buffer was processed, not when the system action was recorded.