0

I'm working on a project In which I need to get the old PID of a process back in Windows OS.

Old PID means the PID which was given to a particular process (for example Notepad was given PID 100) before system restarts. Now after restarting system, I need the older PID(100) AND start that process again.

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe");

Here's my example code ,please provide solution if possible or any other way I can get the old PID back and old process back.

Passer By
  • 19,325
  • 6
  • 49
  • 96
  • 2
    Unless windows records these things in its event/security logs or you log them as you start your application or before you restart your computer, I dont think it's possible. – FreudianSlip Apr 10 '18 at 06:25
  • Check out this link: https://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id – Robert Kock Apr 10 '18 at 06:36
  • Finding the ID **before** the system restarts and writing it to a file which then gets read **after** the system restarts is already part of what you plan to do, isn't it? – Yunnosch Apr 10 '18 at 06:39
  • 1
    Please explain more about the role of each of the three tagged languages. – Yunnosch Apr 10 '18 at 06:40
  • You can restart the process just as you mentioned within your example code – Robert Kock Apr 10 '18 at 06:40
  • 3
    Why do you need to get back the old PID after system restart? This looks like an [XY Problem](http://xyproblem.info/) – Jabberwocky Apr 10 '18 at 06:42
  • Please don't spam tags, this is clearly just Java – Passer By Apr 10 '18 at 07:08
  • Because I want restart the application like notepad in which user works before system restart. – Patel Vikash Apr 10 '18 at 07:21
  • 1
    Another thing - you wont be able to nominate a PID prior to a process starting. The OS will allocate the PID at the time - you cannot tell the OS to start an application and to use a specified PID. – FreudianSlip Apr 10 '18 at 07:26
  • 3
    @PatelVikash just restart the application like in your code snippet, what's wrong with that? Why do you care about the PID? – Jabberwocky Apr 10 '18 at 07:28
  • @Michael Walz Because I am working on a project Ram-Reloader that enables you to reboot Windows without losing your place in your work. It builds a checklist of currently open applications, and will restart the apps you've selected the next time you logon to the computer. – Patel Vikash Apr 10 '18 at 09:00
  • @Michael Walz please suggest any other way to doing this. – Patel Vikash Apr 10 '18 at 09:02

2 Answers2

1

On unix systems, it is common practise to create a simple text file named .pid in the same directory as the scripts to start and stop the application. It contains the pid number of the running process.

If the application crashes (or the system restarts without closing it down cleanly), the .pid file will remain.

When restarting an application, although the pid number contained in the .pid file is no longer relevant from an operational perspective, it can be handy to know that the process crashed (or at least didnt shut down cleanly) - usually the start script for the operation will check for a .pid file and not start the application until this file is removed manually.

So you could do something similar in your case. You would need to "wrap" the process / service startup and shutdown to facilitate this.

Here's a question about that: What is a .pid file and what does it contain?

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
0

You should store a list of open applications (names/exe paths), not their PIDs, as the old PIDs will be meaningless after the process has terminated/aborted. Upon restarting the application, a new PID will be assigned by the OS, which you as a programmer will have no control over.

Brishna Batool
  • 445
  • 3
  • 15