Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem here is that each new process started by bash gets new PID and you have to redirect the output of that process before starting it. But you cannot tell what PID will be assigned to that process by OS.</p> <p>The solution to this problem is not to start a new process, but replace the existing bash process with a new one using <code>exec</code>.</p> <p>Here is an example. First, we write a basic C program that prints its PID:</p> <pre><code>// printpid.c #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; int main() { printf ("C process pid is %d\n", getpid()); return 0; } </code></pre> <p>Then we write a simple bash script that will print its PID and replace itself with this program using exec:</p> <pre><code>#!/bin/bash # printpid.sh echo Bash process PID is $$ exec ./printpid &gt; $$.log </code></pre> <p>Now, let's write a script that will call this <code>printpid.sh</code> script multiple times:</p> <pre><code>#!/bin/bash # example.sh ./printpid.sh ./printpid.sh ./printpid.sh </code></pre> <p>Now, let's make sure it works:</p> <pre><code>$ ls example.sh printpid printpid.c printpid.sh $ ./example.sh Bash process PID is 6397 Bash process PID is 6398 Bash process PID is 6399 $ ls 6397.log 6398.log 6399.log example.sh printpid printpid.c printpid.sh $ cat 6397.log C process pid is 6397 $ cat 6398.log C process pid is 6398 $ cat 6399.log C process pid is 6399 $ </code></pre> <p>Be aware that when you are using <code>exec</code> you cannot put anything else after that in the script as bash shell replaces itself with a new process specified as command line arguments for <code>exec</code>.</p> <p>Good luck hacking!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload