Bash – Is it possible for a child script to write output to a parent process

bashkeepalived

I have a notify script kicked off by keepalived daemon process whenever it's state changes.

The problem is, echo output from my notify script does not appear in the output displayed when running keepalived with the -l (log to local console) option

Normally, I could write this output to a file and that would be a fine solution, except that in this case the keepalived daemon is running in a docker container and I want the docker daemon to see the log of all output including those from my notify script.

This is what I have tried for my notify script:

#!/bin/bash

MY_PPID=$(ps -o ppid= $$)
MY_PPID=${MY_PPID// }

function echo
{
  builtin echo $(date +"%b %e %H:%M:%S"): $1 >> /proc/${MY_PPID}/fd/0
}

echo "Notify state=$3"

And the output from keepalived:

Starting VRRP child process, pid=8847
Interface queue is empty
No such interface, ib0
No such interface, ib1
No such interface, docker0
Interface queue is empty
No such interface, ib0
Registering Kernel netlink reflector
No such interface, ib1
No such interface, docker0
Registering Kernel netlink command channel
Registering gratuitous ARP shared channel
Registering Kernel netlink reflector
Initializing ipvs 2.6
Registering Kernel netlink command channel
Opening file '/etc/keepalived/keepalived.conf'.
Configuration is using : 5879 Bytes
Opening file '/etc/keepalived/keepalived.conf'.
Configuration is using : 61747 Bytes
Using LinkWatch kernel netlink reflector...
Using LinkWatch kernel netlink reflector...
VRRP_Instance(vip) Transition to MASTER STATE
VRRP_Instance(vip) Entering MASTER STATE
Opening script file /etc/keepalived/notify.sh

If it worked I would have expected to see an additional line:

Notify state=MASTER

But obviously it didn't work. Any ideas on how I could get this to work correctly?


I think I have found the issue. MY_PPID does not exist as a process anymore. It would seem that when executing the script the keepalived process does a double fork to detach the script. When I write to the fd of one of the keepalived processes it does work. I can get the pid from the file /var/run/keepalived.pid.

I'll try this out and report back.

Best Answer

Why /proc/${MY_PPID}/fd/0? It usually is the console output...

Can you identify the logfile on the /proc/${MY_PPID}/fd/ of the supervisord process?