How to write a daemon in Unix?

Hi

I have a directory where sometimes a file will come (in a name format say file001.txt).
I want to run a job (.ksh file) as soon as a new file comes into the directory.

How can I write a shell script which will run in the background and monitor arrival of new file in the directory?

Thanks in advance.

Look in to cron jobs, you can write a script to monitor files, depending on what you to do. Put this script in a cronjob, the cron will wake up run the script. The cron wakes up once every minute, and looks for jobs to run.

HTH,

Nitin

Here's a brute force method using ksh:

while true; do
  if [[ -s file001.txt ]]; then
    # Found the file; do stuff here
    print "found file!"
    break # stop the loop
  fi
done

The loop cycles forever, checking whether the file exists, and stops checking once found. You may want to consider the relative effect (if any) of having a script running constantly like this. A cron job that runs every minute might be a better choice, depending on your requirements.

i would suggest the above script with a sleep interval in between for every check.

HI Glenn i tired this code it gives me an error "Can't find file" & still echoes the output desired, i would also like to add sleep intervals within the loop can you explain or send the code to achieve that

Hi all,Matrix,Glenn
can you help me in setting a script which checks for a file within every 10 mins,...script should have time intervals in between
below code checks the file but gives an error msg cant find file & then again prints the output found file

while true; do
if [[ -e file001.txt ]]; then
# Found the file; do stuff here
print "found file!"
break # stop the loop
fi
done

this should have been a new thread :wink:

I took out the break line. that would end the script after the 1st time file is found. Sounds like you want a cron that will look for incoming files on a regular basis? sleep 600 is 10 minutes. Of course you'lll need to move the file once found, or the next interation is going to find the old file.

#!/usr/bin/ksh

while true; do
  if [[ -s file001.txt ]]; then
    # Found the file do stuff here
    print "found file!"
   else 
        print "File not found!"
  fi
sleep 600
done

Good evening Denn Thanks a lot for your input!
I tried that but it gives following message
""Can't find file File not found!
Can't find file found file!"""" it says found file once thats pasted into the folder i am checking for.Also it goes in the loop with out breaking i want to break the loop once file is found.
kindly let me know where this is going wrong!
Thanks again....
Thanks & regards
KPIT

If you're not using ksh, the print command is something else entirely -- a command which expects a file argument, and complains "Can't find file <argument>" when the "file" you give it doesn't exist. Try replace print with echo or printf instead.

Scroll back and reread some earlier posts; denn took out the break but it's easy to put back in -- just scroll further back to find a version which has it.

Hi Era,Denn,matrix & all others

Thanks this works probably i didnt realise ksh & sh...
now it gives me the desired results..thanks a lot for your support

I inserted the break & loop i wanted for specific time

Thanks Again.....
Regards
KPIT

Hi all,

I got the previus code working thanks for that i also have an sftp script working,now my reqmnt is once i login to the sftp server i want to check the arrival of file & do some stuff how do i achieve that..the code with this does not work with sftp pls help
Thanks in Advance
KPIT