Hacker News new | ask | show | jobs
by istjohn 2307 days ago
I recently discovered the "watch" command. With an autosave plugin in Vim, I can put some "print()" statements in my code and run "watch python3 mycode.py" in a small Tmux pane to get nearly instant feedback as I debug something. Or if I'm refactoring, I can do "watch pytest". It's pretty sweet.
3 comments

Using “watch” [1] to run your program is a bad idea. The watch command executes a program at regular intervals, by default it runs once every second.

What you should use instead is “inotifywait” [2] to execute your program(s) whenever there is a change. This way the program will run, for example, every time you save your changes. There are many utilities that make use of inotify (the library that powers inotifywait) some of them are fsnotify [3], fswatch [4] and watchexec [5].

[1] https://linux.die.net/man/1/watch

[2] https://linux.die.net/man/1/inotifywait

[3] https://github.com/fsnotify/fsnotify (written in Go — golang)

[4] https://github.com/emcrisostomo/fswatch (written in C++)

[5] https://github.com/watchexec/watchexec (written in Rust)

If you like inotify, you might also like entr [1].

I only found out about it few days ago, but it seems nice so far.

[1]: http://eradman.com/entrproject/

Wow this is amazing. You made my day with this tool!
If you're going to be using any system like this (either watch or inotifywait), be careful. It's a good chance it's going to run in an "intermediate" state when it's not supposed to run and can do harm to your system (delete files or whatever). Bash scripts are especially dangerous, but it applies to any language.

A good idea would be to at least run the script in some kind of sandbox, so you don't accidentally do something dumb.

Watch is great if you are using an hpc cluster as well. Our workload manager is slurm, so 'watch -n5 squeue -u my_username' is a great way to keep track of the progress of batch jobs submitted to a compute node. I put it in an alias.
Thank you — I’ve often times had to run things interactively and this would have saved me so much time and stress.
This is a bad practice. squeue will hit the scheduler directly. If a lot of people do that your scheduler will be busy answering squeue calls, not scheduling. (Saying this just in case we are working on the same cluster ^^)
I started on a Qt4 Mac8 login screen clone. My repl was a similar shell script to rebuild and run on changes. Watch is pretty cool for stuff like that.