How can I execute some command after the named pipe has been readout in other end. I was trying to use trap. But nothing happens. My script is something like below.


  echo "Caught Signal ... cleaning up the background process."

trap cleanup PIPE

if [ ! -p "/tmp/mysqld.init" ]; then
    mkfifo /tmp/mysqld.init

echo  "CREATE DATABASE IF NOT EXISTS test;" > /tmp/mysqld.init &

exec mysqld --init-file="/tmp/mysqld.init"

Note: This is my docker image's entrypoint script and exec is necessary in my case.

  • If the exec is successful, the shell will no longer exist, so there's no way for the shell to write to a closed pipe and no way the trap body would run. What do you want to do after the pipe has been read? Commented Jan 20, 2019 at 15:56
  • I am not sure what do you mean by shell will no longer exist. But in my case as I am creating a docker image mysqld runs as pid 1 for ever. I just want to close the child/background process after the pipe has been read.
    – SkyRar
    Commented Jan 20, 2019 at 16:59


You must log in to answer this question.

Browse other questions tagged .