7

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?

3
  • 1
    Can you be more specific than "it doesn't work" (error messages, log entries)? Also, apparently you're running Linux instead of Unix (if so, you should tag your question appropriately). What distribution and version and kernel version? If it's not Linux or it's an older kernel, it may not have inotify. Jun 1, 2010 at 8:11
  • Sorry for being unclear. It doesn't work as in, incron doesn't seem to run the command. The pdf that should be generated by the command isn't there (or isn't updated appropriately). I'm using Ubuntu 8.10 with the 2.6.27-17 kernel. I believe inotify should be available.
    – ehsanul
    Jun 1, 2010 at 17:01
  • 1
    I've had trouble running complex commands in the incrontab. Just create a script that does what you want and pass the directory and file $@/$# to it from the incrontab entry: /path/to/watch EVENT scriptname $@/$# Jun 7, 2010 at 14:02

4 Answers 4

2

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.

2
  • incron looks good, thanks! But I'm having issues. I updated the question, please take a look.
    – ehsanul
    Jun 1, 2010 at 4:37
  • Whoo, it would be nice to get an actual description on how to do it.
    – Profpatsch
    Oct 25, 2013 at 9:38
0

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.

1
  • Just editing in vim. I guess I can hook up the system command to vim's :w command?
    – ehsanul
    Jun 1, 2010 at 3:36
0

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.

1
  • What I wanted was something more REPL-like. Almost right after I save my TeX file, I'd like to see the PDF updated, so I can see how it changed. I'm not at all knowledgeable about TeX, so seeing the effect my changes have is very helpful.
    – ehsanul
    Jun 1, 2010 at 17:06
0

Install inotify-tools.

To watch a single file and execute a command when it changes:

inotifywait -m --format %w FILENAME | while read file; do
  COMMAND ${file}
done

where

  • FILENAME: name of the file you want to watch
  • COMMAND: command you want to execute on the file change
  • -m: option to listen indefinitely

inotifywait can output more information than just the filename (via --format), check out man 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

convenience-script FILENAME COMMAND

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .