LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Running a script upon a file is added to a directory (https://www.linuxquestions.org/questions/linux-general-1/running-a-script-upon-a-file-is-added-to-a-directory-684429/)

mohtasham1983 11-18-2008 04:14 PM

Running a script upon a file is added to a directory
 
Hi,

I have a CUPS based printer that holds all print job it receives. However, there are certain print jobs that I want to print immediately. The print job file name is the only way of distinguishing such files from others, so it's impossible to configure CUPS to handle such action as far as I know.

I know that all print jobs go to a certain directory on the server, once they're received. That is, I want to know if there's any way a script that takes care of releasing print job, can be ran once a new file is created in a directory.

I tried to use cron job for this purpose, but I cannot set the cron job time period to less than a minute. I need something that can be invoked immediately.

Any idea?

TB0ne 11-18-2008 04:36 PM

Quote:

Originally Posted by mohtasham1983 (Post 3346757)
Hi,

I have a CUPS based printer that holds all print job it receives. However, there are certain print jobs that I want to print immediately. The print job file name is the only way of distinguishing such files from others, so it's impossible to configure CUPS to handle such action as far as I know.

I know that all print jobs go to a certain directory on the server, once they're received. That is, I want to know if there's any way a script that takes care of releasing print job, can be ran once a new file is created in a directory.

I tried to use cron job for this purpose, but I cannot set the cron job time period to less than a minute. I need something that can be invoked immediately.

Any idea?

How about a sleep statement in your bash script? Run it with "nohup <script name> 2>&1&", so it backgrounds and starts a new process. Can get to every 1 second with sleep...........

michelek 11-18-2008 04:42 PM

Quote:

Originally Posted by mohtasham1983 (Post 3346757)
Hi,

I tried to use cron job for this purpose, but I cannot set the cron job time period to less than a minute. I need something that can be invoked immediately.

Any idea?

inotify is tool of choice

mohtasham1983 11-18-2008 04:53 PM

Quote:

Originally Posted by TB0ne (Post 3346786)
How about a sleep statement in your bash script? Run it with "nohup <script name> 2>&1&", so it backgrounds and starts a new process. Can get to every 1 second with sleep...........

Can you explain more about the "2>&1&" option? What do 2 and 1 mean here?

Also, doesn't it use a lot of resources?

michelek 11-18-2008 04:58 PM

Quote:

Originally Posted by mohtasham1983 (Post 3346817)
Can you explain more about the "2>&1&" option? What do 2 and 1 mean here?

Also, doesn't it use a lot of resources?

2>&1 means that error output (2) will be redirected to standard out (1).
and last ampersand says to execute in background.

I suggest You look at inotify wiki page

mohtasham1983 11-18-2008 10:25 PM

Quote:

Originally Posted by michelek (Post 3346795)
inotify is tool of choice

Thank you very much. I installed inotify-tools and using inotifywait, achieved what I was looking for.


All times are GMT -5. The time now is 08:30 AM.