Unix watch file for changes
You can also subscribe without commenting. This site uses Akismet to reduce spam. Learn how your comment data is processed. If you liked this article, then do subscribe to email alerts for Linux tutorials.
If you have any questions or doubts? Related Posts. Splex: npmjs. Please help Thank you Reply. To view logs from terminal, you can use dmesg as shown.
Ravi, Thanks for the reply When I typed dmesg command it opens something but not log file. Kunal, Could you check that the service syslog is running using following command. You forgot the watch command. In which case the log-tool would be smart filter functions Reply. It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search. I want a quick and simple way to execute a command whenever a file changes.
I want something very simple, something I will leave running on a terminal and close it whenever I'm finished working with that file. And then I need to go to that terminal and press Enter , whenever I save that file on my editor. What I want is something like this:. Update: I want something simple, discardable if possible. What's more, I want something to run in a terminal because I want to see the program output I want to see error messages. About the answers: Thanks for all your answers!
All of them are very good, and each one takes a very different approach from the others. Since I need to accept only one, I'm accepting the one that I've actually used it was simple, quick and easy-to-remember , even though I know it is not the most elegant. Simple, using inotifywait install your distribution's inotify-tools package :. The first snippet is simpler, but it has a significant downside: it will miss changes performed while inotifywait isn't running in particular while myfile is running.
The second snippet doesn't have this defect. However, beware that it assumes that the file name doesn't contain whitespace. If that's a problem, use the --format option to change the output to not include the file name:. Either way, there is a limitation: if some program replaces myfile.
Many editors work that way. Alternatively, use another tool that uses the same underlying functionality, such as incron lets you register events when a file is modified or fswatch a tool that also works on many other Unix variants, using each variant's analog of Linux's inotify.
It makes it very easy to specify multiple files to watch limited only by ulimit -n , takes the hassle out of dealing with files being replaced, and requires less bash syntax:. I've been using it on my entire project source tree to run the unit tests for the code I'm currently modifying, and it's been a huge boost to my workflow already. Flags like -c clear the screen between runs and -d exit when a new file is added to a monitored directory add even more flexibility, for example you can do:.
I wrote a Python program to do exactly this called when-changed. FILE can be a directory. Watch recursively with -r. How about this script? It uses the stat command to get the access time of a file and runs a command whenever there is a change in the access time whenever file is accessed. This command will exit when the output changes, ls -lR will list every file and directory with its size and dates, so if a file is changed it should exit the command, as man says:.
But I don't want this solution because it's kinda annoying to type, it's a bit hard to remember what to type, exactly, and it's a bit difficult to undo its effects need to run :au! BufWritePost myfile.
In addition, this solution blocks Vim until the command has finished executing. To display the program output and completely disrupt your editting flow, as the output will write over your editor for a few seconds, until you press Enter , remove the :silent command. If you happen to have npm installed, nodemon is probably the easiest way to get started, especially on OS X, which apparently doesn't have inotify tools.
It supports running a command when a folder changes. This works on FreeBSD. The only portability issue I can think of is if some other Unix doesn't have the mktemp 1 command, but in that case you can just hard code the temp file name.
Have a look at incron. It's similar to cron, but uses inotify events instead of time. Another solution with NodeJs, fsmonitor :. Alternately, you can create a phony target and have the rule for it both call your script and touch the phony target while still depending on your script. You can set it up to watch any number of patterns in your project's directory, and execute commands when changes occur. Good chance even that there's a plugin available for that what you're trying to do in the first place.
If your command takes more than 2 seconds to run, watch will wait until it's done before doing it again. Watchdog is a Python project, and may be just what you're looking for:. Using short arguments, and restricting to only execute when events involve " main.
This version runs inotifywait once and monitors for events. Such that inotifywait doesn't need to be re-executed upon every event encountered. I like the simplicity of while inotifywait Therefor I made a helper script that uses inotifywait without those limitations: inotifyexec. Usage is described by just running the command. Improved Sebastian's solution with watch command:.
A little more on the programming side, but you want something like inotify. There are implementations in many languages, such as jnotify and pyinotify. This library allows you to monitor single files or entire directories, and returns events when an action is discovered.
The information returned includes the file name, the action create, modify, rename, delete and the file path, among other useful information. This is simple and portable. There is another answer based on the same strategy using a script here. Take a look also. Reflex is a small tool to watch a directory and rerun a command when certain files change.
As a few others have done, I've also written a lightweight command line tool to do this. It's fully documented, tested and modular. This command will run the specified command in the argument repeatedly by showing its output and errors. By default, the specified command will run every 2 seconds and watch will run until interrupted. Syntax: watch [options] command Options: -d, —differences: This option highlights the differences between successive updates.
The options will be going to read the optional argument which changes highlight to be permanent, allowing the user to see what has changed at least once since the first iteration. Example: watch -d free -m. Skip to content. Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Watch a file for change Ask Question. Asked 11 years, 1 month ago. Active 3 years, 8 months ago. Viewed 11k times. Update: Check if my. Improve this question.
0コメント