Help with Running More than One Program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Running More than One Program
# 15  
Old 01-21-2010
Quote:
Hi, glad it works out so well. Perhaps you could try the same thing with the server too, see how that pans out:
I tried out your next script too and it works like a charm! I used to spend hours trying to keep track of what was going on with programs. I often had so many terminals up and running it was just plain ridiculous. Also, the processes would never die and it used to mess up the experiments I was trying to run. Truly, thank you very much for all your precious help, Scrutinizer.

My next job will be to go through your scripts and try to comment them out in detail. I will be back with questions, I'm sure. So, stand by. :-)

Gratefully,

DTW

P.S: Also if you PM me your name, I can put it in as the primary author for these scripts. I really don't want to take the credit for work that I've not done.

---------- Post updated at 04:28 PM ---------- Previous update was at 03:53 PM ----------

Let's consider this script, first:
Code:
#!/bin/bash
#
# Usage
# From the agent directory:
# ./run-all-agents AgentName1 AgentName2 ...
#
trap killsubs INT
killsubs()
{
  echo "CTRL-C was pressed"
  jobs -p|xargs kill
  echo "Jobs were killed"
  exit
}

TAC_AGENT_HOME=`pwd`
LIB=${TAC_AGENT_HOME}/lib
CLASSPATH=.
CLASSPATH=${CLASSPATH}:${TAC_AGENT_HOME}/bin
for i in $( ls ${LIB}/*.jar ); do
  CLASSPATH=${CLASSPATH}:$i
done

for i in "$@"
do
  java -server -Xmx1024M -Xms512M -cp $CLASSPATH edu.umich.eecs.tac.aa.agentware.Main -config "config/$i.conf" &
done
wait

OK, so, I'm trying to get an idea of the high-level flow here. According to me this is what is executed first:
Code:
TAC_AGENT_HOME=`pwd`
LIB=${TAC_AGENT_HOME}/lib
CLASSPATH=.
CLASSPATH=${CLASSPATH}:${TAC_AGENT_HOME}/bin
for i in $( ls ${LIB}/*.jar ); do
  CLASSPATH=${CLASSPATH}:$i
done

for i in "$@"
do
  java -server -Xmx1024M -Xms512M -cp $CLASSPATH edu.umich.eecs.tac.aa.agentware.Main -config "config/$i.conf" &
done
wait

This essentially fires up the programs by taking them in as command line arguments and starting them each off as a background process. After it has done this, it waits. Then if Ctrl+C is executed, this piece of the code is executed:
Code:
trap killsubs INT
killsubs()
{
  echo "CTRL-C was pressed"
  jobs -p|xargs kill
  echo "Jobs were killed"
  exit
}

I think the "INT" stands for interrupts (which is caused by the user hitting Ctrl+C). Then, the routine "killsubs()" is called. This first outputs the line "Ctrl -C was pressed". Then it gets all the PIDs of the jobs. I'm not sure what -p does. On the man pages, we have:
Quote:
-p Display only the process IDs for the process group leaders of the selected jobs.
What does that mean? "xargs" looks like it gets all the items from the standard input and then executes the command "kill" on them. Then, we inform the user that the jobs were killed and exit cleanly. Is that a fair first pass?

Thanks,

DTW

---------- Post updated at 05:45 PM ---------- Previous update was at 04:28 PM ----------

I have a quick question:
Code:
trap killsubs INT
killsubs()
{
  echo "CTRL-C was pressed"
  jobs -p|xargs kill
  echo "Jobs were killed"
  exit
}

Is there any way we could nicely print out the names of the jobs we killed? I tried putting in:
Code:
trap killsubs INT
killsubs()
{
  echo "CTRL+C was pressed"
  jobs -p|xargs echo
  jobs -p|xargs kill
  echo "Agents were killed!"
  exit
}

But this printed only the PIDs.

Thanks,

DTW

Last edited by DTriniWay; 01-21-2010 at 05:05 PM..
# 16  
Old 01-22-2010
In my case, the job names were in 4th & 5th position of jobs -l command output. Below code will show the job name to the user before killing it.

Code:
trap killsubs INT
killsubs()
{
  echo "CTRL+C was pressed"
  jobs -l | awk '{ print $4,$5 }'
  jobs -p|xargs kill
  echo "Agents were killed!"
  exit
}

Hope this helps.
-Nithin.
# 17  
Old 01-22-2010
Hey Nithin,

Thanks for your suggestion.
Code:
trap killsubs INT
killsubs()
{
  echo "CTRL+C was pressed"
  jobs -l | awk '{ print $i }'
  jobs -p|xargs kill
  echo "Agents were killed!"
  exit
}

I tried the following piece of code and changed i from 0 right up to 13 but I didn't see the names of my programs anywhere. :-( Is there anything else I can try?

Thanks,

DTW

---------- Post updated at 05:17 PM ---------- Previous update was at 02:41 PM ----------

So, I finally managed to get the names working. I'll post more, later. :-)

DTW
# 18  
Old 01-25-2010
OK, so I'm back. Apparently, adding this line to the script did the trick:
Code:
jobs -p | while read pid;do ps -p $pid -oargs | perl -pe 's/.*?config\/(.*?).conf/$1/';done | grep -v COMMAND

I had a buddy help me write that out. I have no idea what it exactly does in detail, though. :-)
So, the full script is now:
Code:
#!/bin/bash
#
# Usage
# From the "Client" directory type:
# ./run-these-agents AgentName1 AgentName2 ...
#
trap killsubs INT
killsubs()
{
  echo
  echo "CTRL+C was pressed"
  echo "The following agents were killed!"
  jobs -p | while read pid;do ps -p $pid -oargs | perl -pe 's/.*?config\/(.*?).conf/$1/';done | grep -v COMMAND
  jobs -p|xargs kill
  exit
}

TAC_AGENT_HOME=`pwd`
LIB=${TAC_AGENT_HOME}/lib
CLASSPATH=.
CLASSPATH=${CLASSPATH}:${TAC_AGENT_HOME}/bin
for i in $( ls ${LIB}/*.jar ); do
  CLASSPATH=${CLASSPATH}:$i
done

for i in "$@"
do
  java -server -Xmx1024M -Xms512M -cp $CLASSPATH edu.umich.eecs.tac.aa.agentware.Main -config "config/$i.conf" &
done
wait

Thanks to all who contributed in making this script work for me. I'm grateful for your help.

Sincerely,

DTW
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running C program in UNIX

I want to run a C program from my BASH script. Here's some very basic simplified code of my bash script: #!/bin/bash echo "Run C program" ./main.c echo "Ran C program" Here's my main.c: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { ... (3 Replies)
Discussion started by: millsy5
3 Replies

2. Shell Programming and Scripting

Running a program using csh

I have a program which I can run on the command line like below and works fine /nethome/chrisd/HSeis/TommyCD/TommyCD-1101/bin/raytrac vmod=npt10-z30.vmod srfile=jcdint.sc rcfile=jcdint.rc phases="SP FS" level=twop format="X T" dtau=0.1 mdacc=0.5 mindist=0.1 maxitertp=25 ray=npt10-z30.ry... (0 Replies)
Discussion started by: kristinu
0 Replies

3. UNIX for Dummies Questions & Answers

Running a program (Dynflow)

Lets get some stuff out of the way before the question. I am currently running FreeBSD 7.0 on a VirtualBox virtual machine. I do not know much about Unix or FreeBSD, though I do run linux at home. My boss gave me some files that he says are a unix version of the program Dynflow. The Programs... (0 Replies)
Discussion started by: poet_will
0 Replies

4. Programming

running a parallel program

hi , i need to run a parallel program . for example; program1 { array=" the second program should called here : program 2" the execution should continue } the 2nd program should recieve an array of information as argument and it should... (4 Replies)
Discussion started by: bankpro
4 Replies

5. Programming

running a program for a specified time

how can i run a program for a specified time? i need to print a current time during program execution. (3 Replies)
Discussion started by: prosputko
3 Replies

6. UNIX for Dummies Questions & Answers

Running a program on boot!

Hi there! I tried to search for something like this here but couldn't find anything. I need to run a specific program when linux starts up. I need to run it after the rp-pppoe has started because this prog needs internet connection. I start the program by entering ./dynix start (its in my home... (4 Replies)
Discussion started by: D-Lexy
4 Replies

7. UNIX for Dummies Questions & Answers

Running a program

Hi.Iam new to Linux.i got linux 7.0 pro and dont know how to run programs. I want a perl interputer and i know i installed one but how do i run it ??? Also how do i run a C or C++ editor ?and how do i run cron ? (3 Replies)
Discussion started by: perleo
3 Replies

8. Programming

running a c/c++ program in unix

This is not a question, but rather a simple how-to for programmers who are new to the UNIX environment. I too,am new to UNIX. First I developed a few programs on my box and perfected them until they were satisfactory for execution. Problem was however, that once i compiled and all that,... (2 Replies)
Discussion started by: kray
2 Replies

9. Shell Programming and Scripting

Running a program automatically

How can I make a program run automatically at a certain time of day? My problem is I need to make a small backup program that will back up a few files every day? (3 Replies)
Discussion started by: jvadn0
3 Replies

10. Programming

Running a compiled Program

Just getting into the Unix command line programming and am unable to run any program I write. I am using a Makefile and the source is compiling but when I enter the name of the output file I get back: bash: lab01exe.out: command not found I'm sure I am just dooing something simple... (2 Replies)
Discussion started by: Krebsbac
2 Replies
Login or Register to Ask a Question