5

I have a question regarding shell scripts (the environment is Linux, preferably Ubuntu).

We want to execute a stress test on a RESTFul application. The stress test is composed of two processes. Running them could be something like:

java -jar stress.jar

java -jar stress.jar -someparameter somevalue

The two have to be started at the same time.

The first process should start, run, and return. The second too. By definition the second will return much earlier, and we want it to be repeatedly executed until the first one returns.

I would be very thankfor if somebody can provide me the script (or the basics that I can use) for this to achieve.

EDIT

this did the trick:

#!/bin/bash

commandA & apid=$!; 

sleep 10;

while kill -0 $apid; do commandB; done
gyorgyabraham
  • 2,550
  • 1
  • 28
  • 46
  • Testing restful solution -- http://stackoverflow.com/questions/203495/testing-rest-webservices. Using bash to create a test condition may not be stable. – Jayan Jan 05 '12 at 13:07
  • I only want to use bash for "automatically typing" the commands that I already use to start these programs - as you can see our test suite is already implemented in JARs. You mean i can't rely on bash for this simple task? – gyorgyabraham Jan 05 '12 at 13:12

3 Answers3

2

use & operator to start the first process in background:

java -jar stress.jar &

so second process you can start multiple times in foreground while first is running:

java -jar stress.jar -someparameter somevalue 
java -jar stress.jar -someparameter2 somevalue2

but if processes print into stdout, it can be messed.

1

shell - get exit code of background process

has your answer. instead of printing something to stdout, you can run your short-lived command.

Community
  • 1
  • 1
Andreas Frische
  • 8,551
  • 2
  • 19
  • 27
0

Here is another way that should work


#!/usr/bin/expect
spawn java -jar stress.jar -someparameter somevalue
expect -timeout 0 timeout { 
    system java -jar stress.jar -someparameter2 somevalue2
    exp_continue
} 

I believe this is slightly superior to the while loop the OP posted because that suffers from a pid reclamation race condition, which could be serious if the second command lives for a long time.

frankc
  • 11,290
  • 4
  • 32
  • 49