Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is the scenario: A user creates a new file X.php let's say using nano. The user writes some content, saves the file (Stage S1) but does not exit the nano. He again make some changes and then exit the nano after saving the file again (Stage S2).

Question: I've a piece of code(a trigger) that I need to inject in every php file in a directory. I'm able to do so. But in the upper scenario, as soon as I detect a new file(using inotifywait) is created (Stage S1), I inject my code into it. However, since nano was never closed,thus ,in the next save (Stage S2) my injected code gets overwritten by the new changes of user. How can I get rid of this issue?

P.S.: The editor can be any text editor and I don't know the editor used by the user to create file.

share|improve this question
up vote 0 down vote accepted

You need to modify your code injector process to act whenever a PHP file is modified or created, and make it smart enough to check to see whether your code is there, and insert it if it isn’t.  Otherwise you are vulnerable to the following scenario:

  1. User creates X.php, saves it, and exits the editor.
  2. Your process inserts your code.
  3. User edits the file, sees your code, says “WTF?” and deletes it (and saves the file).
share|improve this answer
The 3rd point shall not become real as the other user knows that my code will be there by anyhow. As for your question, my technique is able to inject code whenever a file gets created and I can also parse the files to know whether my code is there or not. However, your comment still doesn't answer my question :( as nano never exits for 1st time, user won't see my code, he overwrites the file in Stage S2 having my code written just after Stage S1. – Arch May 10 '14 at 20:56
But the point of my idea is that your code-injection process should be run again when the user saves the file again -- at S2, and, in fact, every time he saves/modifies the file. – Scott May 12 '14 at 19:33
I think I got your point. I was making things hard perhaps. Thanks – Arch May 13 '14 at 18:26

I'm sorry but editors buffer the editing process in may different ways. Unless you generate your own editor with the desired architecture in mind and force everybody to use that particular editor, you're going to be sorely disappointed and go through a lot of complexities with the problem still remaining.

Wrappers on the editors will not help this problem either. As long as a file is open logically (buffered mode) you will be doomed to failure.

share|improve this answer
From here (… ), I found that audit can also provide you which program created/modified the file. I'm kinda thinking of killing that process, modify my file, open the editor with the file to user. I've to see how this can be achieved. Do you also think this is gonna work! – Arch May 10 '14 at 20:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.