Thanks to @tripleee. I think it is a good solution.
$ cat ./wrap_fork.c
//fork wrapper.c
#include <dlfcn.h>
#include <stdio.h>
#include <unistd.h>
pid_t fork(void){
FILE * f = fopen("/tmp/dearforks","a");
typedef pid_t (*t_fork)(void);
t_fork org_fork = dlsym(((void *) -1l), "fork");
pid_t p = org_fork();
fprintf(f,"pid = %i\n",p);
fclose(f);
return p;
}
$ gcc -fPIC -c -Wall wrap_fork.c
$ gcc -shared wrap_fork.o -ldl -lstdc++ -o wrap_fork.so
now follow.sh
$ cat follow.sh
#!/bin/bash
export LD_PRELOAD=./wrap_fork.so
$* &
now, it's time for execution:
./follow.sh ./main.sh
and the result:
$ cat /tmp/dearforks
pid = 2065
pid = 0
pid = 2066
pid = 0
pid = 2067
pid = 0
It's have a taste of what I want. Except some zeros) (And pids after calling sleep in future:( )
Why there are zeros in the result?