I have developed a Bash script that accepts parameters:

$ head -n 3 MyScript.sh
#!/usr/bin/env bash
# This is my script
#... do something

I call it via CRON at different scheduling and with different parameters:

$ crontab -l
* * * * * MyScript.sh 1 -t 600
*/2 * * * * MyScript.sh 40
*/3 * * * * MyScript.sh 41,42
*/5 * * * * MyScript.sh 61
* * * * * MyScript.sh 21

When wanting to know what instances are running (the script is not very long, just a few seconds of execution):

$ pgrep -f -a -l "MyScript.sh"
25003 /bin/sh -c MyScript.sh 21
25005 /bin/sh -c MyScript.sh 41,42
25006 bash /bin/MyScript.sh 21
25007 bash /bin/MyScript.sh 41,42
25008 /bin/sh -c MyScript.sh 40
25009 bash /bin/MyScript.sh 40
25010 /bin/sh -c MyScript.sh 1 -t 600
25012 bash /bin/MyScript.sh 1 -t 600

We can see in the above example the MyScript.sh 21 (and any other) instance apparently running twice, one of them started via bash and the other one via /bin/sh -c . Both do start and finish at the same time, as I have confirmed with htop.

I am performing these tests in RaspBian (Debian based).

Here below is a example of what happens when running the same script on FreeBSD:

$ pgrep -f -a -l "MyScript.sh"
78230 bash /bin/MyScript.sh 103
78106 bash /bin/MyScript.sh 470
77484 bash /bin/MyScript.sh 2
77430 bash /bin/MyScript.sh 451
77019 bash /bin/MyScript.sh 52
76922 bash /bin/MyScript.sh 101 -v -d
76642 bash /bin/MyScript.sh 1 -t 600
76174 bash /bin/MyScript.sh 102 -v -d

As can be seen, BSD doesn't have these repeated instances.

Why is this happening on RaspBian and how could I solve it? Actually, do I need to "solve" it at all?

  • Please try with ps -ef so that you have the parent/child PID chain.
    – roaima
    Mar 16, 2020 at 21:17

1 Answer 1


Your script is doing something inside of subshells. Example:

#!/usr/bin/env bash

sleep 10
echo outer done
#!/usr/bin/env bash

(sleep 10; echo inner done)
echo outer done

If you run the first one of those, there's no subshell, so you'll only see one of it in the process list. If you run the second one of those, there is a subshell, so it will fork and you'll see two of it in the process list. (If you want more details on your exact situation, then please post the full contents of your script in the question.)

  • Indeed, there are subshells inside the script. That is the answer. Thanks you. Anyway, it could be useful some option to make pgrep behave like it does on FreeBSD (where it hides subprocesses, it seems). Mar 16, 2020 at 21:22
  • 2
    @SopalajodeArrierez I don't think that FreeBSD's pgrep hides subprocesses. But not all shells create subprocesses for (all the) subshells. And many shells optimize (...; cmd) into (...; exec cmd) so only the pid of cmd will be seen in the process table. Even bash does that when it's a simple command: (cmd) => the same as cmd;.
    – user313992
    Mar 16, 2020 at 23:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .