
November 2009 Article Delta Neutral Hedging Strategies
IF YOU HAVE THE MEANS: Please consider donating $10 to the American Cancer Society's RELAY FOR LIFE! The 2010 Drive Ends April 10.
(for the impatient, the script)
Have you ever wanted or needed to suspend execution of a process for a period of time, and then resume execution of the same process for a period of time, and repeat this periodic activity for; a fixed number of cycles, a fixed ammount of time, or until the process completes naturally? And do it from within a bash or other unix shell script?
This bash shell script shows you how to sussped a process and resume the process within the script. It allows you to identify 1 or more pids programmatically, and then will cyclicly suspend the process for X seconds, then resume the process for Y seconds. X and Y are configurable parameters.
./processControl.sh java grep MYAPP 20 30
This will look for a process where "java" and "MYAPP" are both in the command, but "grep" is not in the command, and it will on a periodic basis; force the process to sleep for 20 seconds, and then run for 30 seconds. This will continue until the process either terminates normally, or the LIMIT of cycles has been met.
#!/bin/bash
############################################################################
#
# (c) 2009
#
# Author(s):
# Q. Boiler (q.boiler@quantprinciple.com)
#
############################################################################
#+------------------------------------------------------------------------+
#| |
#| This Script will look for a Pid, |
#| based on grepping for for the existance of 2 fields |
#| and lack of existance of 1 field from a ps auxwww command. |
#| |
#| Once the pid is found, it will suspend the process for some time, |
#| Then it will resume execution for some time. |
#| |
#| Params: $1 filter param 1, a process must contain this on it's |
#| command. |
#| $2 filter param 2, a process MUST NOT contain this |
#| on it's command line |
#| $3 filter param 3, a process must contain this on it's |
#| command. |
#| $4 Suspend Period |
#| $5 Run Period or Resume Period |
#| |
#| Typical Usage: |
#| ./controller.sh java grep EngineProcess 180 120 |
#| |
#| This will look for a process that contains java and EngineProcess |
#| in the command line, but does not contain grep. Once it finds the |
#| process, it will suspend execution for 180 seconds, the resume |
#| execution for 120 seconds. |
#+------------------------------------------------------------------------+
#+--------------------------BONUS MATERIAL!-------------------------------+
#| UNDERSTANDING UNIX SIGNALS: |
#| Unix allows you to send signals to a process, assuming you have |
#| Permission that is. Signals can be handled at multiple levels: |
#| some signals are handled by the scheduler, others are passed to |
#| the underlying process. |
#| |
#| This script uses 3 different Unix Signals: |
#| SIGSTOP - will Suspend a process (at the scheduler level) |
#| SIGCONT - will continue or resume a process. (at the scheduler level)|
#| SIGINT - will pass to the process, for orderly shutdown. |
#| |
#| How do you send a signal to a process? One of the easiest |
#| techniques is to use the kill command. It is interesting to |
#| note that kill just sends signals, if the signals don't cause the |
#| process to be killed, the process won't actually die. In that |
#| sense, kill is really a poor choice of names. |
#| |
#| Example: |
#| kill -SIGSTOP 2333 |
#| Seeing all possible signals: |
#| kill -l |
#+------------------------------------------------------------------------+
var0=0
LIMIT=20
INCLUDEA=$1
EXCLUDE=$2
INCLUDEB=$3
SLEEP=$5
RUN=$6
pid=$(ps auxwww |grep $INCLUDEA |grep $INCLUDEB |grep $EXCLUDE | awk {'print $2 '} )
while [ "$var0" -lt "$LIMIT" ]
do
kill -SIGSTOP $pid
echo "Suspending $pid"
sleep $SLEEP
kill -SIGCONT $pid
echo "continuing execution $pid"
sleep $RUN
done
echo "killing $pid"
kill -SIGINT $pid