paint-brush
Running a process for exactly ten minutesby@atroche
213 reads

Running a process for exactly ten minutes

by Alistair RocheMarch 9th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I wanted to get a quick picture of what the <a href="https://hackernoon.com/tagged/processes" target="_blank">processes</a> on my <a href="https://hackernoon.com/tagged/computerr" target="_blank">computer</a> were doing in the background, so I turned to <a href="https://en.wikipedia.org/wiki/DTrace" target="_blank">DTrace</a> (shout out to Julia Evans for <a href="https://jvns.ca/blog/2016/07/03/debugging-tools-i-love/" target="_blank">getting me excited about it</a>), which can tell me handy things like “who’s opening up which files?” and “what system calls are being made?”

Company Mentioned

Mention Thumbnail
featured image - Running a process for exactly ten minutes
Alistair Roche HackerNoon profile picture

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!