1

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?

Braiam
  • 35,991

2 Answers2

0

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.

Ramesh
  • 39,297
0

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

steeldriver
  • 81,074