Each time I make some changes to a file and save, I'd like a command to run. I can set up a Ruby script that could monitor the file and run the command as I'd like, but it occurs to me that there should be some simple Unix way to do this. Is there? I'm on Ubuntu 8.10, to be slightly less vague.
Edit:
incron
was suggested, and looks good. But I'm having trouble getting it to work (edit: the command doesn't seem to run when I save a file in the specified directory, or my command is faulty). I installed incron and set up my user's incron table using incrontab, and added the user to the allowed list. My specific situation is editing a TeX file. I want the command to render it as a PDF to run every time I edit and save the TeX file. So this is the incron table entry I came up with:
/home/ehsanul/Documents/latex IN_CLOSE_WRITE echo $@/$# | grep \\.tex$ | xargs xelatex
What am I doing wrong?
You don't say which Unix you're using, but Linux has inotify and there are inotify-tools and incron.
There is a Ruby interface to inotify.
Not directly, no (unless there is some kind of hook on a filesystem that I'm unfamiliar with).
Do you edit the file via some standard method? If so, it would be easier to automate the command from that angle, rather than from the system level.
If you tell us more about the situation, we might be able to give you better answers.
If you happen to use version control (and if you don't, you certainly should!), you could set up a commit hook. Just choose a version control program that allows you to do that, most of them do.
Install inotify-tools.
To watch a single file and execute a command when it changes:
where
FILENAME
: name of the file you want to watchCOMMAND
: command you want to execute on the file change-m
: option to listen indefinitelyinotifywait
can output more information than just the filename (via--format
), check outman inotifywait
for what else it can do (it’s a rather short read). Plus, the examples on the wiki give some insight.Somebody should probably wrap these commands in convenience script that does exactly what you asked for with