mesmariusz
Posts: 57
Joined: Fri Feb 15, 2013 6:07 am

pgrep -n motion - difference if run from command and script

Mon Apr 29, 2013 5:43 am

[quote="mesmariusz"]Script:

Code: Select all

#!/bin/sh

pgrep -n motion
returns independent if motion is run or not:

Code: Select all

root@raspberrypi:/home/pi# /home/pi/scripts/motion-act.sh
18411
root@raspberrypi:/home/pi#
root@raspberrypi:/home/pi# /home/pi/scripts/motion-act.sh
18419
root@raspberrypi:/home/pi#
n...
As we can see it returns some random and still increasing value.

Another if it is run directly from command line. Then it is still and the same value when motion is turned on:

Code: Select all

root@raspberrypi:/home/pi# pgrep -n motion
18391
root@raspberrypi:/home/pi# pgrep -n motion
18391
root@raspberrypi:/home/pi# pgrep -n motion
18391
and when turned off:

Code: Select all

root@raspberrypi:/home/pi# pgrep -n motion
root@raspberrypi:/home/pi# pgrep -n motion
root@raspberrypi:/home/pi# pgrep -n motion
Why is works different in these two cases?

I would like to force motion is olways working follow example:
http://ni.recoverydata.pl/content/uruch ... %82a-pgrep

But it will never work due to reason abowe.

Maybe I could get some help here?

Thank you very much.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: pgrep -n motion - difference if run from command and scr

Mon Apr 29, 2013 6:30 am

Because you have called your script "/home/pi/scripts/motion-act.sh".

"pgrep motion" will search for any process name that contains "motion", so it will match your script name and return the PID of that. The reason it gives a different number everytime you run it is because the script has a new PID each time.

Try using "pidof motion" instead, and read the manpages of pgrep and pidof. ("man pidof" or "man pgrep").

Return to “General programming discussion”