I want to save the PID of a nohup process as part of the name of nohup's generated output file.

This is going in the right direction, but only works if the process with it's ID is already settled, i.e. only works at the next line. That means that it does not work, because it's the wrong PID (from the last command):

nohup echo "hello World" > nohup_out_`echo $!`.txt &

Is there any way to save the resulting PID in nohup's output filename?


No. You can't save the PID of the command in the name of the file you're redirecting the output of that command to.

The reason for that is quite simple, your shell opens the file assigned with > or >> before it runs the command. There is no PID yet.

| improve this answer | |
  • Maybe there is a trick getting the next pid which will be assigned? – colidyre Aug 8 '18 at 14:49
  • Although PID's are sequential there is no feasible way to predict with certainty the PID a process will get assigned by the kernel – HBruijn Aug 8 '18 at 14:55

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.