I have a bunch of commands that I start with a start.sh
script, storing their PID. Then later, I want to stop them with a stop.sh
run at the user's convenience.
Watch the trap:
I run start.sh
today, which stores in a file the PIDs15000
I forgot stopping my processes. And a week after, I rebooted my computer.
I run the stop.sh
script, now. It attempts to kill the tasks with PIDs15000
, reading them without thinking from the file.
=> These tasks, if some happen to have these PIDs on my new rebooted system, are no longer the ones I started by mystart.sh
script, and I will put my system into an unknown state.
How, when I catch first the PID of a process with a $$
in a Linux script, may I gather other information to ensure I can have no confusion with another task of same PID that could appear in the future?
Gathering PPID, for example, or start date/time, or something that ensures some kind of "universal uniqueness", if I can write this.. ?
How do you gather process information and how do you kill it without confusion?