Search code examples
bashfileloggingexecpid

Bash exec write output to logfile and write PID to seperate file


I got a problem in the understanding of BASH, writing the output of exec command to a logfile & write the PID to a seperate file. That's what I tried so far:

exec ./node -connect-manager -manager-address $MANAGER_IP:5998 -manager-web $MANAGER_IP:8000 >> /var/log/skywirenode.log >> echo $$ > /home/odrod420/skywireScript/node.pid &

Has anybody got an idea on what I am thinking wrong?

Thanks in advance!


Solution

  • You can't do multiple redirections of stdout in a single statement. The echo statement should be done as a separate statement:

    echo $$ > /home/odrod420/skywireScript/node.pid
    exec ./node -connect-manager -manager-address $MANAGER_IP:5998 -manager-web $MANAGER_IP:8000 >> /var/log/skywirenode.log 2>&1
    

    If you want to run node in the background and save its PID in the file, you shouldn't use exec, you should put & after the command, and echo $! to the file:

    ./node -connect-manager -manager-address $MANAGER_IP:5998 -manager-web $MANAGER_IP:8000 >> /var/log/skywirenode.log 2>&1 &
    echo $! > /home/odrod420/skywireScript/node.pid