When was / proc / PID created?

I am writing a Bash script to keep track of a process and detect when it crashed. For this I control the / proc directory,

start_my_process;
my_process_id=$!;
until [[ ! -d "/proc/$my_process_pid" ]]; do
   # alert the process is dead and restart it...
done

      

Can you guarantee that a process entry in / proc / will be created before Bash finishes executing the command to start the process? Or is it possible that over time my check above will be executed, the entry for start_my_process may not be created yet?

EDIT: In the end, I actually went against the custom solution and chose monit, which is an excellent watchdog tool.

+3


source to share


1 answer


/proc/<pid>

never created. This is not a real directory.

/proc

- virtual file system. When you open one of your "files" and read from its output stream, the data is provided by the kernel. Since the kernel is also responsible for managing the process <pid>

, the kernel will tell you that the directory /proc/<pid>

exists as soon as and as long as the kernel keeps track of it.



Since bash will not be able to install $!

until the process exists, you will definitely be safe to check the virtual directory of the process after that /proc

.

+4


source







All Articles