configuring nats server

550 views
Skip to first unread message

Herbert Helmstreit

unread,
Mar 28, 2023, 8:47:42 AM3/28/23
to nats
Hello,
 I am trying to improve my nats server installation on RHEL Given a start script like this

export NATS_HOME=/root/NATS-Srv

export PID=$NATS_HOME/bin/pid.txt

export LOG=$NATS_HOME/log/server.log

$NATS_HOME/bin/nats-server -c $NATS_HOME/cfg/server.conf &

nats server is started with $PID pointing to the pid file The documentation says “Server configurations can specify variables … resolved from environment variables having the same name." Obviously I do not understand this. In the config file I want to use the environment variable $PID, but I could not figure out how:

pid_file=/root/NATS-Srv/bin/pid.txt

works but this is not what I want. I want to reference all paths relatively or by variables-

pid_file=$PID Parse error on line 26: 'Expected a top-level value to end with a new line, comment or EOF, but got '0' instead.'

pid_file={$PID} Parse error on line 56: 'Unexpected EOF processing map.'

pid_file="$PID" Parse error on line 26: 'Expected a top-level value to end with a new line, comment or EOF, but got '0' instead.'

 pid_file=$PID Parse error on line 26: 'Expected a top-level value to end with a new line, comment or EOF, but got '0' instead.'

pid_file=${PID} variable reference for '{PID' on line 26 can not be found

pid_file="${PID}" Parse error on line 26: 'Expected a top-level value to end with a new line, comment or EOF, but got '0' instead.'

pid_file:$PID Parse error on line 26: 'Expected a top-level value to end with a new line, comment or EOF, but got '0' instead.'

Can somebody help me out, I'm running out of ideas.

Regards

Herbert

Phil Pennock

unread,
Mar 30, 2023, 8:12:27 PM3/30/23
to nats
My apologies for the double mail to Herbert, I accidentally replied from a non-subscribed address the first time.

On 2023-03-28 at 05:47 -0700, Herbert Helmstreit wrote:
>  I am trying to improve my nats server installation on RHEL [...]

Below, I answer your question, but before I do so: have you considered
using one of the systemd .service files which are included in the
nats-server source tree?

https://github.com/nats-io/nats-server/blob/main/util/nats-server-hardened.service

That's a good starting for a well-run nats-server install.  You could
add to the [service] section something like:

    EnvironmentFile=-/etc/defaults/nats-server

and then define any additional variables you want available.

We really don't recommend running the nats-server as root.  Even if the
nats-server is probably far safer than many other things run as root,
it's not a good idea to run _anything_ with more privilege than it
needs.  The only reason I can think of that you might even want to is
for binding port 443 for websockets support, and you can solve that in
systemd with:

    AmbientCapabilities=CAP_NET_BIND_SERVICE

>  [...] Given a start

> script like this
>
> export NATS_HOME=/root/NATS-Srv
>
> export PID=$NATS_HOME/bin/pid.txt
>
> export LOG=$NATS_HOME/log/server.log
>
> $NATS_HOME/bin/nats-server -c $NATS_HOME/cfg/server.conf &
>
> nats server is started with $PID pointing to the pid file The documentation
> says “Server configurations can specify variables … resolved from
> environment variables having the same name."

If I run this:
  export NATS_HOME=$HOME/NATS-Srv
  mkdir -pv $NATS_HOME/bin $NATS_HOME/log $NATS_HOME/cfg
  echo 'pid_file=$PID' > $NATS_HOME/cfg/server.conf
  export PID=$NATS_HOME/bin/pid.txt
  export LOG=$NATS_HOME/log/server.log
  nats-server -c $NATS_HOME/cfg/server.conf

then nats-server starts up using the pid-file as expected; the single
quotes keep PID from being expanded, and in fact I write out the
configuration file before the variable is defined.

$ cat -nv $NATS_HOME/cfg/server.conf
     1 pid_file=$PID

If I replace $PID with $POD then the error message is:

    nats-server: variable reference for 'POD' on line 2 can not be found

So the nats-server (v2.9.15 in my case) works fine.

> *pid_file=$PID* Parse error on line 26: 'Expected a top-level value to end
> with a new line, comment or EOF, but got '0' instead.'

I think that there was a typo in your configuration file.

> *pid_file={$PID}* Parse error on line 56: 'Unexpected EOF processing map.'

Curly braces define a map/dictionary/key-value-paired item.

> *pid_file="$PID"* Parse error on line 26: 'Expected a top-level value to
> end with a new line, comment or EOF, but got '0' instead.'

The $dollar expansion does not happen inside quoted strings in the nats
server configuration, so I would expect this to create a file named
"$PID" (4 characters, no quotes) in the current directory when the
nats-server is started.

>  *pid_file=$PID* Parse error on line 26: 'Expected a top-level value to end
> with a new line, comment or EOF, but got '0' instead.'

and so on.

Are you sure that the pid_file directive is actually on line 26?

The only way I can get that sort of error message out of the nats-server
is to have a line which actually has a digit 0 after the closing
delimiter.  For instance:

   server_tags []0
   log_file="foo"0

So I think that you're having some text editor issues.
Reply all
Reply to author
Forward
0 new messages