# Unix



## cgjoker (Aug 13, 2003)

Anyone have any ideas on how I can create a Unix script that basically waits for two 'triggers' before running?

In a nutshell, I have unix script a which sends a triggers to unix script z. I have another unix script b which also sends a trigger to unix script z.

Unix script z doesn't execute its commands later in its script until both triggers from unix script a and b are received.

thx.


----------



## Guy (Feb 19, 1999)

Take a look at the 'trap' command, I am sorta a unix newbie myself, but I think this can do something similar to what you want.


----------



## codejockey (Feb 11, 2002)

There are several things to consider when trying to synchronize multiple processes (scripts, in your case). For example:


will script a and script b continue to execute after sending the trigger to script z?
does script z need to detect a trigger immediately after it is sent, or can there be a delay of a few seconds or so?
will script a and script b have any knowledge of script z (e.g., will they know its name, etc.)? 
what scripting language are you using? For example, perl offers access to far more system resources (shared memory, etc.) than does your basic shell script.
Here's one design based on shell scripts that requires script a and script b to remain around after setting their triggers:

script a, b, and z are started
script z scans a known directory for a known filename from script a and script b
script a completes processing and writes a file with its own PID to a known directory.
script b completes processing and writes a file with its own PID to a known directory.
when script z detects the files from both script a and script b, it verifies that script a and script b are both still active (this is necessary in case there are old files from script a or script b). You can use the kill command and the PID from each trigger file to verify that the triggers have been written by active processes.
script z completes processing, possibly also terminating script a and script b (since script z knows the PID of each trigger script, it can signal them as needed).

Of course, there are other possible designs. This one is very basic but still offers a reasonable degree of robustness.

Hope this helps.


----------



## cgjoker (Aug 13, 2003)

The trap command to my understanding is used with specific signals that are dependent on user actions. What I need is this script to basically run only when two files have been written, or, a command is sent from 2 applications and received from this script.

The second reply is warrant of investigation. However, using your suggested principal, how can I write a script that basically looks for 2 files and if they both exist, it will then execute?


----------



## cgjoker (Aug 13, 2003)

I figured id try to do to scripts, both the same, that check for 2 file. If both files exist a trigger is sent to execute the next application.

For some reason though, im getting the error:

y.sh[24]: [/mdm_work/cg_test/campopt_test/finalfile/file_a.dat: not found.

here is my code:
fa=/mdm_work/cg_test/campopt_test/finalfile/file_a.dat
fb=/mdm_work/cg_test/campopt_test/finalfile/file_b.dat

if ["$fa" "$fb"]
then
/affinium_prd/v311/bin/unica_svradm -p 4884 -s iw103e -x "RUN -p /mdm_work/cg_test/campopt_test/ac_sessions/ac_4.ses -u impctid -S MDMDB -U impctid -P trigger8"
else
echo "upon running y.sh both files weren't available" >> /mdm_work/cg_test/campopt_test/z_y_log.log
fi


----------



## cgjoker (Aug 13, 2003)

here was my problem...

if test -f $fa && test -f $fb

thanks for the ideas... cg.


----------

