├── .gitignore ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── index.js ├── kp.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | package-lock.json 31 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "vsicons.presets.angular": false 3 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 alfred sang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kp 2 | 3 | kp is a tool for kill process by server port. it can be used on mac && linux && window 4 | 5 | [![npm version](https://badge.fury.io/js/kp.svg)](http://badge.fury.io/js/kp) 6 | 7 | ## Install 8 | 9 | $ [sudo] npm install -g kp 10 | 11 | ## Usage 12 | 13 | default server port is 3000 14 | 15 | ``` 16 | $ kp 17 | ``` 18 | 19 | or kill by some port 20 | 21 | 22 | ``` 23 | $ kp 3002 24 | ``` 25 | 26 | or with sudo 27 | 28 | ``` 29 | $ kp 3002 -s or kp 3002 --sudo 30 | ``` 31 | 32 | ## Code 33 | 34 | kill by some port 35 | 36 | ``` 37 | #!/usr/bin/env node 38 | 39 | var kp = require("kp"); 40 | kp(3980); 41 | ``` 42 | 43 | or 44 | 45 | kill by some port with sudo 46 | 47 | ``` 48 | #!/usr/bin/env node 49 | 50 | var kp = require("kp"); 51 | kp(3980, 'sudo'); 52 | ``` 53 | 54 | ## Contributing 55 | 56 | 1. Fork it 57 | 2. Create your feature branch (`git checkout -b my-new-feature`) 58 | 3. Commit your changes (`git commit -am 'Add some feature'`) 59 | 4. Push to the branch (`git push origin my-new-feature`) 60 | 5. Create new Pull Request 61 | 62 | ## 版本历史 63 | 64 | - v1.1.0 实现可编程调用 65 | - v1.0.0 初始化版本cli,实现kp导出 66 | 67 | ## 欢迎fork和反馈 68 | 69 | - write by `i5ting` i5ting@126.com 70 | 71 | 如有建议或意见,请在issue提问或邮件 72 | 73 | ## License 74 | 75 | this repo is released under the [MIT 76 | License](http://www.opensource.org/licenses/MIT). 77 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var debug = require('debug')('kp'); 2 | var os = require('os'); 3 | 4 | function exec_kp(server_port, pre) { 5 | if (arguments.length == 1) { 6 | pre = ""; 7 | } 8 | 9 | var child_process = require('child_process'); 10 | var script = os.type() == 'Windows_NT' ? `netstat -ano | findstr ${server_port}` : pre + ' lsof -i:' + server_port + '|xargs killall'; 11 | 12 | debug(script); 13 | 14 | // execFile: executes a file with the specified arguments 15 | child_process.exec(script, 16 | function (error, stdout, stderr) { 17 | if (error !== null) { 18 | console.log('Failed to kill process on port ' + server_port + ':' + error); 19 | } else { 20 | if (os.type() == 'Windows_NT') { 21 | stdout.split('\n').filter(function (line) { 22 | var p = line.trim().split(/\s+/); 23 | var address = p[1]; 24 | if (address != undefined) { 25 | if (address.split(':')[1] == server_port) { 26 | child_process.exec('taskkill /F /pid ' + p[4], function (err, stdout, stderr) { 27 | if (err) { 28 | return console.log('Failed to kill process on port ' + server_port + ':' + error); 29 | } 30 | console.log('Killed process on port ' + server_port); 31 | }); 32 | } 33 | } 34 | }); 35 | } 36 | else { 37 | console.log('Killed process on port ' + server_port); 38 | } 39 | } 40 | }) 41 | } 42 | module.exports = exec_kp; 43 | -------------------------------------------------------------------------------- /kp.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var argv = process.argv; 4 | argv.shift(); 5 | 6 | var file_path = __dirname; 7 | var current_path = process.cwd(); 8 | 9 | var server_port = 3000; 10 | var pre = ''; 11 | 12 | for(var i in argv){ 13 | var _argv = argv[i]; 14 | if(_argv == '-s' || first_arg == '--sudo'){ 15 | pre = 'sudo '; 16 | } 17 | } 18 | 19 | if ( argv.length > 1 ) { 20 | // console.log(argv) 21 | var first_arg = argv[1]; 22 | if ( first_arg == '-h' || first_arg == '--help' ) { 23 | return console.log('Usages: kp is a tool for kill process by server port. only use for mac\n\tkp \n\tkp 3002\n\tkp 3002 -s\n\tkp 3002 --sudo'); 24 | }else{ 25 | server_port = (argv[1] + '').trim(); 26 | } 27 | } 28 | 29 | // exec_kp (server_port); 30 | var kp = require("./index"); 31 | 32 | kp(server_port, pre); 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kp", 3 | "version": "2.0.0", 4 | "description": "kp is a tool for kill process by server port. only use for mac && linux", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "npm publish .", 8 | "test": "DEBUG=kp node test.js" 9 | }, 10 | "preferGlobal": "true", 11 | "bin": { 12 | "kp": "kp.js" 13 | }, 14 | "keywords": [ 15 | "node", 16 | "npm", 17 | "kill", 18 | "process", 19 | "linux", 20 | "mac" 21 | ], 22 | "repository": { 23 | "type": "git", 24 | "url": "git+https://github.com/i5ting/kp.git" 25 | }, 26 | "author": "", 27 | "license": "ISC", 28 | "bugs": { 29 | "url": "https://github.com/i5ting/kp/issues" 30 | }, 31 | "homepage": "https://github.com/i5ting/kp", 32 | "dependencies": { 33 | "debug": "^2.2.0" 34 | }, 35 | "maintainers": [ 36 | { 37 | "name": "i5ting", 38 | "email": "i5ting@126.com" 39 | } 40 | ] 41 | } 42 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 4 | var kp = require("./index"); 5 | 6 | kp(3980, 'sudo'); --------------------------------------------------------------------------------