I have created a basic shell script that simply counts down from 1000. This is just for testing but could be any application/process.
##filename:test.sh##
#!/bin/bash
i=1000; while [ $i -gt 0 ]; do echo $i; i=`expr $i - 1`; sleep 1; done
Which I start running thus:
sh test.sh
I now need to get: a) the pid of that script, b) the state of that script
If I do
pidof sh test.sh
I get multiple results though.
Running
ps aux | grep test.sh
I get multiple entries including some that are Terminated (state=T) and one that is grep test.sh
.
How can I restrict the pidof
to just the one(s) I need (assume it is just the one instance that is running)
I also need the state. I tried running:
ps aux | grep test.sh | ps -o stat --no-headers
but that didn't work. I get the state, but for multiple items