0

When I do

node process.js 1>$LOGFOLDER\\$$.log

the created process's PID become the filename. I want to run this process in the background as I want to run many instances of the same process.js. So, I added & at the end,

node process.js 1>$LOGFOLDER\\$$.log &

This made the process to run in the background but the PID(the filename) is always wrong. I did log the process id using $! after the above line. In first case both $!'s output and logfile's name are same. But in second case they are not. Making the process to run in background using & is giving different PID in the $$ placeholder. Why is this happening and how can I make the both work

1 Answer 1

1

That is because the backgrounded process runs in a subshell. To do what you want you could do this:

LOGFOLDER="$LOGFOLDER" bash -c 'node process.js 1>$LOGFOLDER.$$.log' &
3
  • Oh Thanks. But this one is not executing the process.js. No file is created and no error logs Commented Mar 25, 2017 at 13:11
  • @RaR: It's those single quotes. Look at my edited answer for a resolution. HTH. Commented Mar 25, 2017 at 13:13
  • 1
    Without LOGFOLDER being exported (I see the edit does this in the one-line format) then it's set to nothing in the subshell; if you do an ls -a then you'll see the log file as a hidden .12345.log file in the current directory (if you have write access to it) Commented Mar 25, 2017 at 13:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.