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


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' &
| improve this answer | |
  • Oh Thanks. But this one is not executing the process.js. No file is created and no error logs – RaR Mar 25 '17 at 13:11
  • @RaR: It's those single quotes. Look at my edited answer for a resolution. HTH. – user218374 Mar 25 '17 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) – Stephen Harris Mar 25 '17 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.