Ricky
Ricky

Reputation: 777

console kill command does not work on Ubuntu

Hello I have this command on my Nodejs application which finds a process with the trim name and kills it.

let killTrim = () => {
  console.log('kill "$(pgrep -f ' + trimName + '.mp4)"')
  cmd.run('kill "$(pgrep -f ' + trimName + '.mp4)"')
  trimName = null
}

this works perfectly fine on localhost on my computer but won't work on my server (ubuntu 16.04). The console log is outputted but the cmd.run command doesn't run on the server. When I manually put in 'kill "$(pgrep -f moo.mp4)"' on the server it will run and kill the process.

I've done some research on the issue but I haven't been able to find anything issues similar to this.

Upvotes: 3

Views: 1264

Answers (3)

Tarun Lalwani
Tarun Lalwani

Reputation: 146500

I would use something simple like below

cmd = require('node-cmd');
cmd.get('pgrep -f node | xargs kill', (err, data, stderr) => {
  console.log(err, data, stderr);
});

It will work even when you have multiple processes running

Processes terminated

Upvotes: 3

Vasyl Moskalov
Vasyl Moskalov

Reputation: 4630

I looked on your pastebin. Looks like that pgrep -f ... return more than one pid. So try this one:

cmd.run('for a in $(pgrep -f '+trimName+'.mp4); do kill $a; done');

Upvotes: 1

Alik Khilazhev
Alik Khilazhev

Reputation: 1107

It seems to be that your node application does not have enough permission to kill the process. You can try the following:

sudo node app.js 

Upvotes: 1

Related Questions