0

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?

colidyre
  • 137
  • 6

1 Answers1

1

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.

HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • Maybe there is a trick getting the next pid which will be assigned? – colidyre Aug 08 '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 08 '18 at 14:55