0

Am hosting a jar file in GCP and everytime I make a change in the file ofcourse i would need to update the jar, but everytime I run the with the command nohup java -jar java.jar &>> logy.log & this run the file in background and gives a processes ID which is required to kill the process if I want to rerun the file when I make some changes. But once I restart the linux server I cannot find the process with the command bg and I tried looking for several other commands on google but none seem to have help. I need a consitent way of killing the process rather than writting the process ID somewhere everytime I run the file.

sahil
  • 3
  • 2
  • 1
    Does this answer your question? [How can a Java program get its own process ID?](https://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id) – Jeff Holt Aug 23 '21 at 00:24

2 Answers2

1

Can be use process API, that added in Java 9. Process id can be obtain from:

long process_id = ProcessHandle.current().pid();
Mihai8
  • 3,113
  • 1
  • 21
  • 31
0

Generally, applications create a PID file that includes the PID and then kill with that info. If you don't want to use this approach, maybe you can go with pkill.

omerkarabacak
  • 124
  • 1
  • 9