Are we there yet… Get notified when processes end with PWATCH
I four-finger swipe my touchpad… a mostly black terminal slides into my field of view and before the screen snaps in place I can plainly see that my bzip2 remains working with no bar showing progress. Yes, I could have piped my process through pv
for a progress bar, and/or added a && say "Finally Finished"
, but alas, I did not and with upwards of 10 minutes already sunk into zipping I wasn’t going to ^C
it now!
What I really need is a notification when my process is complete, so I don’t need to swipe over every 2 minutes. I quickly search for any cli tool to throw a notification when an existing process dies, but to no avail. How hard could it be to poll ps PID
of my process and throw a mac notification when it’s done? I four-finger swipe again back to an unchanged terminal screen bzip2
ing. Thats it! I’ll make it myself!
And now it begins…
I switch tabs to a fresh terminal screen.
$ cd ~
$ mkdir pnotify# wait... let me check if that name exists...
$ curl -sI https://www.npmjs.com/package/pnotify | grep HTTP
HTTP/1.1 200 OK# hmm.. how about
$ curl -Is https://www.npmjs.com/package/pwatch | grep HTTP
HTTP/1.1 404 Not Found$ mv pnotify pwatch
$ cd pwatch
$ npm init -y
$ git init
$ vim .
A bit of code later and I’ve got a v0.0.1
complete!
npm publish
npm install -g pwatch
Now, pwatch PID
will call a function that runs ps PID
, if it sees the process is running, then it runs the main function again after 1 second, otherwise it will send a native notification through node-notifier
.
Great now let me just ps aux | grep bzip
to find the PID of the file being zipped… Nothing?! I switch my tabs back to see that my file has finally finished zipping! 😫 😐
Okay… well, now I’ve got another opportunity to try it out for my scp file.bzip server:~
this will surely take a while. Hmm but wouldn’t it be easier if I can just scp file.bzip server:~ | pwatch
since I know this will take a while? Switching back to my pwatch
directory, I add some code to handle reading stdin
and viola a v0.0.2
is born!
Now I scp largefile.zip server:~ | pwatch
check my email and HackerNews and several minutes later I get a native notification alerting me my process is complete!
Now it ends…
Over the next weekend I make a few more adjustments. With some feedback and support from friends Mirza Joldic and Tom Goldenberg I decide on a couple of things.
- Add a
pwatch search NAME
command to find running processes. - Add a
pwatch help
to display documentation for everything - Have
pwatch PID
run in background by default. (before0.0.6
it would hang the terminal)
Now if you want native notifications when your processes end:
npm install -g pwatch
And see the full list of commands it offers with pwatch help
If you have any suggestions, issues, or requests related to pwatch
I would absolutely love to hear about them! Either here, twitter, or as a GitHub issue: jkrup/pwatch and while you’re there feel free to leave a ⭐️ … :)