Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I wanted to know if I can execute a command from the terminal and right after the command started I send its PID to another command to be monitored.

I need to do something like this,

dd if=/path/of/file of=/path/of/destination

And then I need to monitor its execution using pidstat, but pidstat takes a pid and I don't know how to make one send the pid to the other.

I need to monitor disk usage, memory usage, cpu usage and time elapsed. Does any one know of a way to do that?

share|improve this question

marked as duplicate by Mikel, jasonwryan, Ramesh, Braiam, derobert Jun 12 at 5:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 0 down vote accepted

In bash (and possibly other shells) you can capture the PID of the last backgrounded process programatically using the $! shell variable i.e. it should be possible to do something like

dd if=/path/of/file of=/path/of/destination & pidstat -p $!

See this similar askubuntu question

share|improve this answer
    
Thank you for the answers guys, both get the job done but this one its more practical and easier to understand –  cesar.rickinho Jun 12 at 4:24

You can do it by appending a & to the end of your command.

dd if=/path/of/file of=/path/of/destination &

When you do the above command, it will give you a pid and you can use the pid to monitor.

Testing

cat infinite.sh
#!/bin/bash
while :
do
        echo "Press [CTRL+C] to stop.."
        sleep 1
done

Now, in the first terminal I run the above command as below.

-bash-3.2$ ./s.sh &
[1] 25666
-bash-3.2$ Press [CTRL+C] to stop..
Press [CTRL+C] to stop..
Press [CTRL+C] to stop..
Press [CTRL+C] to stop..

Now, I can open the second terminal and just use kill -9 25666 to stop this process. In your case, you can open the second terminal to monitor the dd command using the pid that you got in the first terminal.

share|improve this answer

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