Skip to content

Commit 72dbb29

Browse files
committed
Run pwatch PID in background
1 parent 918f291 commit 72dbb29

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Add `pwatch` at the end of your command.
4040
4141
${'$ pwatch 4030'.cyan}
4242
43-
– Run in background with a &
43+
– Run a chained process in background with a &
4444
4545
${'$ sleep 10 | pwatch &'.cyan}
4646
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pwatch",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Process Watcher",
55
"main": "./index.js",
66
"bin": {

src/checkPid.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import ps from 'ps-man';
2+
import notifier from 'node-notifier';
3+
4+
export default function checkPid(pid, cb = () => { notifier.notify(`Process ${pid}: Completed`); }) {
5+
ps.list({ pid }, (err, res) => {
6+
if (res.length === 0) {
7+
return cb();
8+
}
9+
return setTimeout(checkPid.bind(this, pid, cb), 1000);
10+
});
11+
}

src/main.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@ import tty from 'tty';
22
import notifier from 'node-notifier';
33
import ps from 'ps-man';
44
import 'colors';
5-
6-
function checkPid(pid, cb = () => { notifier.notify(`Process ${pid}: Completed`); }) {
7-
ps.list({ pid }, (err, res) => {
8-
if (res.length === 0) {
9-
return cb();
10-
}
11-
return setTimeout(checkPid.bind(this, pid, cb), 1000);
12-
});
13-
}
5+
import { spawn } from 'child_process';
146

157
function handlePiped() {
168
process.stdin.on('data', () => { }); // required so that readable is called on end
@@ -52,7 +44,7 @@ function help() {
5244
5345
${'$ pwatch 4030'.cyan}
5446
55-
${'–'.grey} Run in background with a &
47+
${'–'.grey} Run a chained process in the background
5648
5749
${'$ sleep 10 | pwatch &'.cyan}
5850
`);
@@ -68,7 +60,11 @@ function handlePid(pid) {
6860
} else if (Number.isNaN(Number(pid))) {
6961
help();
7062
} else {
71-
checkPid(pid);
63+
// Run in BG
64+
spawn('node', ['dist/runCheckPid.js', pid], {
65+
stdio: 'ignore',
66+
detached: true,
67+
}).unref();
7268
}
7369
}
7470

src/runCheckPid.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import checkPid from './checkPid';
2+
3+
checkPid(process.argv[2]);

0 commit comments

Comments
 (0)