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

  • Silly question, but pn what screen, and how exactly is updating noip and pythun running related? – Jacob Vlijm Jan 2 '17 at 23:42

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:

enter image description here

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 of 1 negates to 0 , thus keeping the loop running.
  • Once the process appears, exit status becomes 0 , it's negated to 1, causing for loop to terminate and display message with echo

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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