In my linux startup scripts, when I start a process like (openvpn used for this example, but the question is general to any process);
openvpn --config /etc/myserver.conf
what is the best way to find it and be 100% certain it's the right process and kill it in the stop section? I normally use stuff like:
pid=$(ps -efww | grep -v grep | grep openvpn | grep /etc/myserver.conf | awk '{print $2}')
Sure it works nearly all of the time, but sometimes there are issues with accidentally matching processes with nearly the same name (e.g. myserver.conf-new), so I'm looking for a better approach.
- Some processes have a way to store the pid somewhere, that's fine, but generally I'm skeptical of killing processes solely based on a pid sitting in a file somewhere.
- Solaris has projects, it's not all roses in my limited experience because you have to set up /etc/projects first, but it does make it easy to tag and later find processes.
- Maybe using the environment, like setting an environment variable like (MYID=myserver) and then looking for it with
ps e -ef | grep MYID=myserver
? Still may suffer the same problem with accidental matching.
I wish there were something easy like:
launch --tag myserver openvpn --config /etc/myserver.conf
and
pgrep --tag myserver