Hi guys,

I have a simple question, I want to store the output of the following command:

sudo iotop -t 0 -C 3 | grep "load: "

As you can see it is running all the time, and we get a new line every 3sec.

I just want to store these new lines into a single variable, so I can use it into a script.

To clear the screen, and do not print lines after the others when iotop is running just delete "- C".

I DO NOT WANT TO STORE THE OUTPUT ON THE DRIVE!!!
I DO NOT WANT TO KILL IOTOP, IT MUST BE RUNNING ALL THE TIME IN BACKGROUND!!!

The idea is to do not write on the drive! Just store the output into a single variable called $Activity.

For example:

Activity=$(sudo iotop -t 0 3 | grep "load: ") &

While $Activity... do blahblah... done

But the fact is that nothing can be stored into Activity :(
So I have tried to know if something can be done to read the last output of a specified PID. But I need help on that point too :-/ I don't know how to do it.

Please help... :(

You're not saying what you really want to do here. You can pipe the information to a named pipe or to a remote mount and store the information there.

Obviously you are wanting to do something with the data on another terminal or with another user account... please fill us in.

You're not saying what you really want to do here. You can pipe the information to a named pipe or to a remote mount and store the information there.

Obviously you are wanting to do something with the data on another terminal or with another user account... please fill us in.

Thanks a lot, that was a bit stupid :D

I'm coming back soon with my code ;)

Ok, here is my code:

sudo iotop -t 0 -C 3 | grep "load: " | script.sh

script.sh:

#!/bin/sh

  while read data
  do
      echo "[$(date +"%D %T")] $data" >> log_activity.txt
  done

But nothing is read :(

Use /dev/stdin

sk@sk:/tmp$ echo "hello" | ./script.sh
sk@sk:/tmp$ ls
aquota.group  aquota.user  log_activity.txt  lost+found  script.sh
sk@sk:/tmp$ cat log_activity.txt
hello
sk@sk:/tmp$ cat script.sh
#!/bin/bash

cat /dev/stdin >> log_activity.txt

Also you can use tail -f /dev/stdin instead of a while () loop to read the data.

SOLVED!!!

...
iotop -t 0 -C 3 | grep "load: " --line-buffered |  while read data
do
    echo "${data}"
    #Do whatever you want with ${data} here :)
done
...

:)

Ah that is a good approach to, nice work.

Please mark this thread as solved if you have found an answer to your problem and good luck!

This question has already been answered. Start a new discussion instead.