Running a process for exactly ten minutes

Written by atroche | Published 2017/03/09
Tech Story Tags: programming | bash | linux | mac

TLDRvia the TL;DR App

I wanted to get a quick picture of what the processes on my computer were doing in the background, so I turned to DTrace (shout out to Julia Evans for getting me excited about it), which can tell me handy things like “who’s opening up which files?” and “what system calls are being made?”

Just to start, I decided to run this one-liner to see which programs were making the most system calls:

sudo dtrace -n 'syscall:::entry { @num[execname] = count(); }'

After running the above for a couple of minutes (and browsing idly in the background), I got this output (truncated for brevity):

Now, I want to reboot into a relatively clean state, and run the command for exactly ten minutes, without having to manually stop it. This is what I came up with:

The ampersand makes DTrace run as a background job. Running jobs -p gives you back a list of the process IDs of jobs:

bash-3.2$ jobs -p10038

I pipe that through tail -1 to take only the last one (in case you had other jobs running or suspended in the same shell instance). And then because kill doesn’t take input from stdin, we pipe the PID toxargs, which will pass its stdin as arguments to kill. And we make sure that we run kill with sudo , because that’s how the DTrace process was started.

By the way, because jobs is a bash builtin, if you want help with it you need to use help jobs , not man jobs .

Let me know if there’s a better way of doing this! I’m by no means a bash expert.

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!


Published by HackerNoon on 2017/03/09