简体   繁体   中英

Kill process by process id only if it is a node process

I am killing a process by process id, which works, however, if the process id gets killed somehow and a new process starts taking that process id it will be killed.

So, my thought is to only kill a process if it is a node process which sounds less destructive than just killing any process by process id.

The problem is, node doesn't seem to have a way to get process information by process id. So, is there a way I can get this process information so when I do a kill I only kill node processes?

let settings = require('./settings.json')
let pid = settings.pid
os.platform() == 'win32' ? process.kill(pid) : process.kill(-pid)

You could execute the ps command from nodejs and check that the command column is "node":

ps -p <PID> 

you will need to parse the result of that and check what the CMD column is if it's "node" then that should mean that it's a nodejs process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM