I want to see if some python process is running. I've set up a python script that updates noip, and a cron runs it every 1 minute, but it takes only 2 seconds to run and finish, so I'd like to know if there's a way to do ps -e | grep python and wait until some python process appear in the screen
If your goal is simply to observe is process appears, you can just do this:
watch -n1 "ps -ef | grep command | grep -v grep"
Personally , I frequently use polling technique with pgrep
and while
loop like so:
while ! pgrep -f command ; do true; done && echo "Process started"
This is a good approach if you want to run additional command if a process appears or disappears.
Test run:
The way this works is very simple:
- loop runs
pgrep -f command
on each iteration and negates its exit status. Thus if process not found the exit status of1
negates to0
, thus keeping the loop running. - Once the process appears, exit status becomes
0
, it's negated to1
, causing for loop to terminate and display message withecho
This is a pretty universal approach, works with any shell and scripting language. For instance, I used it in a python script that detects ip change and script to run commands when process appears