Link to home
Start Free TrialLog in
Avatar of GAUTAM
GAUTAMFlag for United States of America

asked on

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.
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Hi,

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.

wmp
Avatar of GAUTAM

ASKER

@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.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GAUTAM

ASKER

@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!

wmp
Avatar of GAUTAM

ASKER

@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