Setting up a Daemon in Unix

I have scheduled a crontab job in AIX 6.1 OS to run twice in an hour which runs for the whole day to process a load.

The load which crontab kicks off needs files to arrive at a particular directory and if the files arrive, I process them.

It so happens that for the 24 times the crontab kicks off my load there would be only 7 times I actually receive the files and for the rest of the times I receive no files and the load reports saying that no files received and I cannot preict when files arrive as it can arrive anytime.

I heard that a DAEMON can be setup in Unix which can monitor for files arriving at a particular destination and call other scripts after that.

I was thinking if it were possible to create a daemon to monitor a particular destination and if files are present, It can call a shell script.

Is this possible, if yes how can I code the daemon. I have no prior experience with daemons.

Can anyone provide a sample for the same.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


please clarify:

Do you want to kick off the load when there are any files in the destination (i. e. when the directory is not empty), or when additional files arrive, or when existing files change (in size/date etc.)?

AIX does not have something like "inotify", unfortunately, and there isn't an Open Source equivalent for AIX either, so we will have to create a script.

If you're planning to test for a nonempty directory we could go without any daemon - just change the crontab entry like this:

0,30 * * * * [[ $(ls /destination | wc -l) -gt 0 ]] && /start/the/load

Consider running the job more frequently - if there are no files nothing will happen, and the additional CPU or I/O cost will be minimal.

gaugetaAuthor Commented:
@woolmilkporc : Thanks for the reply.

To want to answer your question, Yes I want to kick off the load when files arrive in the directory. Most of the time this directory will be empty.

I was thinking if I create a shell script and in the beginning of the shell script I have the logic for checking if there are any files and if there are any files, I will kick off the load or else I will exit the script.

I was thinking If I place the above logic in a while true; do ...; done loop which sleeps for 30 mins and If I start this script as a nohup in background will this give the same effect.

Your  suggestion seems viable too.

I'm just wondering which one will be the best option.

Can you suggest regarding the same.
The crontab solution will not require changing the original script or creating an additional one.
The original script or an additional script do not have to be called at all if there are no files, all the work will be done by cron.

OK, a script looping in the background doesn't cost much either, but why reinvent the wheel? cron is there to do the job.

Anyway, a looping script:

while :
    if ls /destination/* >/dev/null 2>&1 ; then /start/the/load ; fi
    sleep 1800

This is functionally the same as my cron solution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

gaugetaAuthor Commented:
@woolmilkporc: Thanks for the comment.

Can you explain this line:

    if ls /destination/* >/dev/null 2>&1 ; then /start/the/load ; fi
"if" tests the returncode of the following command.

"ls" returns a "true" value if it can successfully list files (i.e. in our case if there is any file under "/destination") and it returns a "false" value if it doesn't find anything to list (i.e. if "/destination" is empty.

We redirect the standard output as well as the standard error to /dev/null because we neither want to see the files if present nor do we want to see the error message if there are no files - we're just interested in the return value.

What follows after "then" is only executed if the result of the previous command (the one tested by "if") is "true".

"fi" terminates the "if" construct.

The semicolons between the elements of the construct get us rid of the need to place each element on a new line - in fact it acts as a command separator just as a new line would.

Thanks for the points!

gaugetaAuthor Commented:
@woolmilkporc : Thanks for the reply.

What would be the substitute for the foll line in TC Shell.

    if ls /destination/* >/dev/null 2>&1 ; then /start/the/load ; fi
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.