Skip to content

Commit 528ed85

Browse files
committed
docs: update usage examples
1 parent 14fad84 commit 528ed85

File tree

2 files changed

+67
-107
lines changed

2 files changed

+67
-107
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Neekey
3+
Copyright (c) 2024 Webpod
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

readme.md

Lines changed: 66 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -25,142 +25,102 @@ This module invokes different tools to get process list:
2525

2626
### lookup()
2727
Searches for the process by the specified `pid`.
28-
```javascript
29-
var ps = require('@webpod/ps');
28+
```ts
29+
import {lookup} from '@webpod/ps'
3030

3131
// A simple pid lookup
32-
ps.lookup({pid: 12345}, function (err, resultList) {
32+
lookup({pid: 12345}, (err, resultList) => {
3333
if (err) {
34-
throw new Error(err);
34+
throw new Error(err)
3535
}
3636

37-
var process = resultList[0];
38-
37+
var process = resultList[0]
3938
if (process) {
40-
41-
console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments);
39+
console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments)
4240
} else {
43-
console.log('No such process found!');
41+
console.log('No such process found!')
4442
}
45-
});
46-
43+
})
4744
```
4845

49-
Define a `RegExp/string` to filter the results by `command` and/or `arguments` match:
50-
51-
```javascript
52-
var ps = require('@webpod/ps');
46+
Define a query opts to filter the results by `command` and/or `arguments` predicates:
47+
```ts
48+
lookup({
49+
command: 'node', // it will be used to build a regex
50+
arguments: '--debug',
51+
}, (err, resultList) => {
52+
if (err) {
53+
throw new Error(err)
54+
}
5355

54-
// A simple pid lookup
55-
ps.lookup({
56-
command: 'node',
57-
arguments: '--debug',
58-
}, function(err, resultList ) {
59-
if (err) {
60-
throw new Error( err );
56+
resultList.forEach(process => {
57+
if (process) {
58+
console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments);
6159
}
60+
})
61+
})
62+
```
6263

63-
resultList.forEach(function( process ){
64-
if( process ){
65-
66-
console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
67-
}
68-
});
69-
});
64+
Unix users can override the default `ps` arguments:
65+
```ts
66+
lookup({
67+
command: 'node',
68+
psargs: 'ux'
69+
}, (err, resultList) => {
70+
// ...
71+
})
72+
```
7073

74+
Specify the `ppid` option to filter the results by the parent process id (make sure that your custom `psargs` provides this output: `-l` or `-j` for instance)
75+
```ts
76+
lookup({
77+
command: 'mongod',
78+
psargs: '-l',
79+
ppid: 82292
80+
}, (err, resultList) => {
81+
// ...
82+
})
7183
```
7284

7385
### kill()
7486
Eliminates the process by its `pid`.
7587

76-
```javascript
77-
var ps = require('@webpod/ps');
88+
```ts
89+
import { kill } from '@webpod/ps'
7890

79-
// A simple pid lookup
80-
ps.kill( '12345', function( err ) {
81-
if (err) {
82-
throw new Error( err );
83-
}
84-
else {
85-
console.log( 'Process %s has been killed!', pid );
86-
}
87-
});
91+
kill('12345', err => {
92+
if (err) {
93+
throw new Error(err)
94+
} else {
95+
console.log('Process %s has been killed!', pid)
96+
}
97+
})
8898
```
8999

90100
Method `kill` also supports a `signal` option to be passed. It's only a wrapper of `process.kill()` with checking of that killing is finished after the method is called.
91101

92-
```javascript
93-
var ps = require('@webpod/ps');
102+
```ts
103+
import { kill } from '@webpod/ps'
94104

95105
// Pass signal SIGKILL for killing the process without allowing it to clean up
96-
ps.kill( '12345', 'SIGKILL', function( err ) {
97-
if (err) {
98-
throw new Error( err );
99-
}
100-
else {
101-
console.log( 'Process %s has been killed without a clean-up!', pid );
102-
}
103-
});
104-
```
105-
106-
you can use object as the second parameter to pass more options:
107-
108-
```js
109-
ps.kill( '12345', {
110-
signal: 'SIGKILL',
111-
timeout: 10, // will set up a ten seconds timeout if the killing is not successful
112-
}, function(){});
113-
106+
kill('12345', 'SIGKILL', err => {
107+
if (err) {
108+
throw new Error(err)
109+
} else {
110+
console.log('Process %s has been killed without a clean-up!', pid)
111+
}
112+
})
114113
```
115114

116-
Notice that the nodejs build-in `process.kill()` does not accept number as the signal, you will have to use string format.
117-
118-
119-
You can also pass arguments to `lookup` with `psargs` as arguments for `ps` command(Note that `psargs` is not available in windows):
120-
121-
```javascript
122-
var ps = require('@webpod/ps');
123-
124-
// A simple pid lookup
125-
ps.lookup({
126-
command: 'node',
127-
psargs: 'ux'
128-
}, function(err, resultList ) {
129-
if (err) {
130-
throw new Error( err );
131-
}
132-
133-
resultList.forEach(function( process ){
134-
if( process ){
135-
console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
136-
}
137-
});
138-
});
139-
115+
You can also use object notation to specify more opts:
116+
```ts
117+
kill( '12345', {
118+
signal: 'SIGKILL',
119+
timeout: 10, // will set up a ten seconds timeout if the killing is not successful
120+
}, () => {})
140121
```
141122

142-
Lastly, you can filter a list of items by their PPID by passing a PPID to filter on. You will need to pass in a `psarg` that provides the PPID in the results (`-l` or `-j` for instance).
143-
144-
```javascript
145-
var ps = require('@webpod/ps');
146-
147-
// A simple pid lookup
148-
ps.lookup({
149-
command: 'mongod',
150-
psargs: '-l',
151-
ppid: 82292
152-
}, function(err, resultList ) {
153-
if (err) {
154-
throw new Error( err );
155-
}
156-
157-
resultList.forEach(function( process ){
158-
if( process ){
159-
console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
160-
}
161-
});
162-
});
163-
```
123+
Notice that the nodejs build-in `process.kill()` does not accept number as a signal, you will have to use string format.
164124

165125
## License
166126
[MIT](./LICENSE)

0 commit comments

Comments
 (0)