1

I have a bash script running as a su cron every 10 minutes. On top of the script I declare:

#!/bin/bash

In crontab I am running it with /bin/bash. I do the same with all the scripts called from my main script.

I noticed that two threads are created:

/bin/bash /main/script
/bin/sh -c /main/script

Also all other scripts that are called get their own single thread (only bash).

Could you explain this behavior to me?

4

Cron uses a shell to run the commands. In that user's case that's /bin/sh, so for a line like:

* * * * * /bin/bash /some/script

Cron runs:

/bin/sh -c '/bin/bash /some/script'

If the shell set was /bin/bash, it would run:

/bin/bash -c '/bin/bash /some/script

Now why don't you see the bash -c process with bash? Bash, when given a single, simple command, directly execs the command instead of forking and execing:

$ strace -fe clone,execve bash -c 'bash foo.sh'
execve("/bin/bash", ["bash", "-c", "bash foo.sh"], [/* 15 vars */]) = 0
execve("/bin/bash", ["bash", "foo.sh"], [/* 15 vars */]) = 0
clone(Process 466 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f93bda10a10) = 466
[pid   466] execve("/bin/true", ["/bin/true"], [/* 15 vars */]) = 0
[pid   466] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=466, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++

With dash, which is /bin/sh:

$ strace -fe clone,execve dash -c 'bash foo.sh'
execve("/bin/dash", ["dash", "-c", "bash foo.sh"], [/* 15 vars */]) = 0
clone(Process 473 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f7f31650a10) = 473
[pid   473] execve("/bin/bash", ["bash", "foo.sh"], [/* 15 vars */]) = 0
[pid   473] clone(Process 474 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f408d740a10) = 474
[pid   474] execve("/bin/true", ["/bin/true"], [/* 15 vars */]) = 0
[pid   474] +++ exited with 0 +++
[pid   473] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=474, si_status=0, si_utime=0, si_stime=0} ---
[pid   473] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=473, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++

You can see the extra clone here.

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.