0

I have an entrypoint script that gets called in my Dockerfile's entrypoint:

ENTRYPOINT ["/bin/sh", "-c" , "/var/run/fmx/Scripts/entrypoint.sh"]

As part of this script, it retrieves a certain value from a file (a port number, 5140).

Checking the logs, using kubectl logs < pod-name >, I can see that the value does get retrieved from the file and printed:

eccd@director-0-ccd-c16c002:~> kubectl logs < pod-name >
5140
rsyslogd: unknown priority name "" [v8.2106.0]
rsyslogd: module 'imfile' already in this config, cannot be added  [v8.2106.0 try https://www.rsyslog.com/e/2221 ]

In the script, I am doing the following:

#!/bin/bash
export RSYSLOG_LISTEN_PORT=$(sed -nE 's/.*port="([^"]+)".*/\1/p' /etc/rsyslog.d/0_base.conf)
echo $RSYSLOG_LISTEN_PORT

However, inside the pod, printenv | grep RSYSLOG_LISTEN_PORT returns nothing, showing the env var doesn't get set. I think this is due to the script being ran as a child process, and being unable to affect the parent process: https://stackoverflow.com/a/496777/18397787

Is there any way I can achieve having the value being set as an env var?

HollowDev
  • 111
  • 1
  • 8
  • A process cannot affect the env vars of its parent process without its explicit cooperation (which is how most "virtual environment" activation functions work). Env vars are private to each process, not global. I don't use Kubernetes but presumably they have some documentation for how to inject env vars into each VM. – Kurtis Rader Aug 18 '22 at 02:47

1 Answers1

0

Inside your entry script /var/run/fmx/Scripts/entrypoint.sh, call your port finding script this way:

. /path/to/port/finder.sh

I set up a test script to demonstrate:

# cat print.sh
#!/bin/sh

export RSYSLOG_LISTEN_PORT=1010
# . ./print.sh
# echo  $RSYSLOG_LISTEN_PORT
1010

Alternatively, you could just use the shell builtin declare in bash or zsh by adding this to your entrypoint.sh

declare RSYSLOG_LISTEN_PORT=$(sed -nE 's/.*port="([^"]+)".*/\1/p' /etc/rsyslog.d/0_base.conf)
James Risner
  • 5,451
  • 11
  • 25
  • 47
  • 1
    Or you can just use `declare` command. `x=100; y=999; declare "$(echo x="'$y'")"; echo "$x"`. – Darkman Sep 19 '22 at 18:47