I have a shell-script in which I do an

iostat -c 1  > data.cpu. &

Later in the script, after I've collected the data I'm interested in, I do a

pkill iostat

which kills iostat and allows me to continue by processing data.cpu and generating a report written to stdout. Part of the report includes

[1]+  Terminated              iostat -c 1  > data.cpu

produced by the pkill. For aesthetic reasons I would like to prevent the "Terminated" message from appearing. I tried various redirects, but so far I haven't succeeded.

Is there any way to prevent this part of the output?


@Quasímodo suggested I look at https://stackoverflow.com/q/81520 which I did not find helpful. However, one of the suggestions in that post pointed to https://stackoverflow.com/q/5719030/1997354

which used disown (man page) to exactly solve my problem. Namely:

iostat -c 1 > data.cpu &
# collect required data
pkill iostat

In addition to using disown there is a way to suppresses the "Terminated" message without using disown -- namely use kill instead of pkill, as in the following

iostat -c 1 > data.cpu &
# collect required data
kill -9 $iostat_pid

As explained in the answer to the question you linked, the displaying of such messages as those can be controlled at a wait command.

So one approach might be to run iostat in background and immediately wait for it with a redirected stderr, and run such entire compound command again in the background so as not to wait on wait.

That is:

(iostat -c 1 > data.cpu & wait 2>/dev/null) &

After that, you can pkill iostat as you were doing originally.

Be careful in fact to target precisely that iostat process, and not the shell’s sub-process being that iostat’s parent. A pkill iostat kills all iostat processes currently running, so that suffices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.