gyorgyabraham
gyorgyabraham

Reputation: 2608

Shell script for running multiple processes

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

Upvotes: 5

Views: 5204

Answers (3)

frankc
frankc

Reputation: 11473

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.

Upvotes: 0

Andreas Frische
Andreas Frische

Reputation: 9891

shell - get exit code of background process

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

Upvotes: 1

Nikolay Polivanov
Nikolay Polivanov

Reputation: 677

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.

Upvotes: 2

Related Questions