Comments (14)
I also want the function
from fswatch.
Hi @svenax, @GithubMachineUser, the reason of this change is keeping things simple and flexible and it got into fswatch
when it merged with fsw
.
The typical use case will be feeding notifications emitted by fswatch
to other programs and I believe the current approach is simpler and more flexible than the previous implementation. In fact, you can run any command when a notification is found piping the output of fswatch
to the command you want to run:
$ fswatch [opts] [args] | do-something
That's also why the -0
option was implemented: so that fswatch
output can be parsed without worrying about file names containing characters such as spaces and new lines.
Probably, the easiest way to get what you want is using fswatch
together with xargs
this way:
$ fswatch -0 [args] | xargs -0 -n 1 [opts] [args]
Basically, this is what's going on:
-0
tellsfswatch
to end a notification record with aNUL
character.-0
tellsxargs
to parse the output accordingly.-n 1
tellsxargs
to execute the specified command every argument is read from the output.
Another approach is using the shell read
builtin specifying the delimiter character (check the documentation of the shell you're using):
$ fswatch -0 [opts] [args] | while read -d "" event \
do \
[do something with the $event] \
done
Hope this helps.
Cheers,
-- Enrico
from fswatch.
That helps. Maybe it is obvious, but the examples I've seen for inotifyd
and similar all use terribly complex while read ...
constructs that I don't want to deal with.
My main use for fswatch
is to sync changes to a remote server. For this I call rsync
to do the real work. Works great. However, If I do something like
fswatch -0 . | xargs -0 -n1 ./sync-files.sh
my sync script will unnecessarily be called for every file changed, instead of just once for all files in the change batch. I guess I need something that throws away the paths printed by fswatch
so I then can call my script. I tried
fswatch . | cat - > /dev/null | ./sync-files.sh
but that hangs after the first invocation. I guess cat
does not get an EOF so it knows when to stop reading from stdin.
Any tips on how to do this? Thanks.
from fswatch.
I went ahead and hacked fswatch
to have a one-set
option that just prints a single throw-away message when a set of changes has been detected. This is probably not useful for any one but me, and I'm sure there are better ways of doing it anyways. But here it is if any one is interested:
https://github.com/svenax/fswatch/compare/alandipert:master...one-set
from fswatch.
Hi @svenax, yeah, I've seen people doing terrible things with while read
.
Since xargs
knows nothing about fswatch
, it can't possibly have a knowledge of a "change batch".
I can only think of one "quick" options at the moment, that is updating sync-files.sh
so that:
- It writes a lock file and a sentinel file when it starts.
- It checks whether the lock file exists: if it exists, it touches the sentinel file it and exits, otherwise, it creates both the lock and the sentinel.
- Before exiting, it checks whether the sentinel is newer than the lock file: if it is, it restarts, otherwise it exits.
I recognise that this use case translates to more complex scripting, but since fswatch
can already watch multiple directories, I still see drawbacks in giving the option of emitting one fake event every time the FSEvents
callback sends a group of changes:
- On the one hand, the use case you propose would "work" out of the box.
- On the other hand the fake event would defy the ability of watching multiple directories.
from fswatch.
@svenax, it seems we were commenting at the same time.
As explained in my previous comment, I thought about the possibility of emitting a "fake" event, but I think it has some drawbacks. I'll have a look at this use case: thanks for the changeset.
from fswatch.
Right, I thought of that too, but my present use-case uses only one directory so that is not a problem. It would of course be possible to emit a single event for each watched directory; the directory name for instance. That is probably reasonably simple to do. I may take a look at that unless you feel like doing it yourself. :)
from fswatch.
That could be a reasonable solution. I'll have a look at the events of the FSEvents API to be sure I'm taking into account all the event flags. Thanks for the offering, I'll have a look myself anyway but if you'd like to contribute, please feel free to do so.
Reopening this issue.
from fswatch.
👍 for an option to only emit some kind of batch change event.
from fswatch.
👍
While I like that you're making it a more flexible tool, running a script / rebooting something in development is probably 90% of the users of this script, and that should be dead simple to do. Thanks!
from fswatch.
👍
Though the change looks good because it's according to UNIX philosophy, I'm pleased if there is the option which allows specifying a command.
from fswatch.
Hi @svenax, @GithubMachineUser, @slindberg, @bwiklund, @nobeans and everybody else.
I've just released v. 1.3.3 where:
- The
-o
option instructsfswatch
to only print one line with the number of the change events received in the last "batch". This way, you can use this event to trigger the execution of another program, as discussed in this issue and in Issue 24. - The
fswatch-run
script has been added to ease the transition from v. 0.x to 1.x. This scripts accepts the olderfswatch path command
syntax and invokesfswatch -o
piping its output toxargs
for you. Being a one-liner, I still strongly suggest adapting your script to the new behaviour.
Cheers,
-- Enrico
P.S.: @svenax, thanks for your changeset.
from fswatch.
Awesome, thanks!
For some reason it looks like the source code tarball in that release doesn't have the 1.3.3 changes 😕
I'm attempting to upgrade the Homebrew Formula, but before I make a PR, is it safe to use the release version with the configure
script already autogen'd?
from fswatch.
Hi @slindberg,
Thanks, you're right, the tag was set but develop was not merged into master. I've updated the tags and the release and now the source code tarball you linked in your comment is correct.
By the way, since you're asking, I use to attach the distribution tarball made by make distcheck
in the release and that should be the way to go. Ironically, in this case it was "the" correct one.
Cheers and let me know if everything's ok,
-- Enrico
from fswatch.
Related Issues (20)
- broken exclude on macos? HOT 1
- Configuration header leaked into public interface in v. 1.17.0 HOT 8
- How to get the PDF documentation HOT 3
- Universal (Arm + x86) library build for MacOS on M1 HOT 1
- Ask for help HOT 1
- import error in silicon mac
- Additional waiting period after --one-event
- Make fails on Solaris 11.4 cbe with gcc-10 or gcc-11
- Trailing slash for directories HOT 1
- CMake package config file is not installed
- fswatch fails to build on windows 11 using cygwin
- Installation for windows missing
- libfswatch README copyright looks stale
- Help configuring fswatch to act only on full file being written by SFTP clients HOT 1
- fswatch command fires multiple events for a single move operation HOT 4
- Which event type should I look for if I am watching a Google Drive directory for new files?
- NetBSD support
- Is there any way to get previous filename for Rename event?
- Symlink question: Is fswatch suppose to notify of linked file changes HOT 1
- execute cmd for single filetype
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fswatch.