3

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?

  • [How to suppress Terminated message after killing in bash?](https://stackoverflow.com/q/81520) may be helpful – Quasímodo May 15 '20 at 17:32
  • That output is not produced by pkill, but by the shell. Start from incorrect assumptions, reach wrong conclusions (No, disown doesn't change a process "from a background to a foreground process"). –  May 16 '20 at 12:02

2 Answers2

2

@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 &
disown
# 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 &
iostat_pid=$(pidof)
# collect required data
kill -9 $iostat_pid
1

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.

LL3
  • 5,270
  • 7
  • 20