BASH Execution Delay / Speedup


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH Execution Delay / Speedup
# 22  
Old 01-29-2015
Quote:
Originally Posted by gmark99
...

Does this work?

Code:
           NUM_PROCS=`ps -u root | grep my_job | wc -l`

            if [ ${NUM_PROCS} -lt ${MAX_NUM_PROCS} ]
            then
                my_job "${CMD_INPUT}" &
            else
                sleep 1
            fi
        else

...
It looks like it should work just fine (although it creates an unneeded process every time through the loop) if you want to completely ignore some command input read from your input file, never run those commands through my_job, and do not want any record of the fact that you chose to ignore those input file command lines.

Every additional process you run, slows down your system for all users on your system and slows down your script (and reduces the number of input lines you'll be able to process). This is an expected side effect when you need to run a process. It is just wasteful when you create processes that are not needed!

For example, consider changing:
Code:
           NUM_PROCS=`ps -u root | grep my_job | wc -l`

to:
Code:
           NUM_PROCS=`ps -u root | grep -c my_job`

changing:
Code:
        cat /home/gmark/rje/COMMANDS.csv | grep "A[BL][AE]" > ${LOCAL_CSV}
        > ${REMOTE_CSV}
        LOCAL_CSV_SIZE=`wc -l ${LOCAL_CSV} | sed "s;^ *;;" | sed "s; .*;;"`

to:
Code:
        grep "A[BL][AE]" < /home/gmark/rje/COMMANDS.csv > ${LOCAL_CSV}
        > ${REMOTE_CSV}
        LOCAL_CSV_SIZE=`wc -l < ${LOCAL_CSV}`

etc.

You still haven't given any indication about what type of system you're using, how many CPUs (or cores) are in your system, and what else might be running on your system that needs run while you're running this stuff. Without that information, I have to assume that setting MAX_NUM_PROCS to 400 will run significantly slower than setting MAX_NUM_PROCS to the number of cores on your system -2.

I would also assume that waiting for a job to complete once in a while instead of running a loop that runs ps and grep throttled only by a sleep 1 would consume considerably fewer resources and actually complete more jobs. (But of course that would require you to look at some of the other suggestions that have been made suggesting other ways to limit the number of background jobs you're running simultaneously.)
This User Gave Thanks to Don Cragun For This Post:
# 23  
Old 01-29-2015
BASH Execution Delay / Speedup

Some of the coding decisions I've made are to allow rapid prototyping and debugging, such as "read" statements in the loop to allow me to step through it slowly. Eventually this will all be removed or changed.

I think the idea of putting a "wait" command in there occasionally is a great idea!

Inside "my_job", I'm basically making several database queries and then using these to decide whether to make a hardware operation, which might take minutes. "my_job" leaves some output data and status in a small file that I go around periodically to collect, and possibly spawn a new "my_job" to respond to.

Is there a possibility of the "my_job" just hanging that there is a more elegant way of checking and purging? I'd hate to have a "wait" command in line that has 20 jobs all of which will complete and then the 21st stops the system by hanging.

Is there some way of handling this, such as a "wait" that has a "time limit"?
# 24  
Old 01-29-2015
@ Don, might this mean counting cpus or threads?

Code:
$ grep -i cores /proc/cpuinfo              
cpu cores	: 4
...

4 - 2 = 2 paralell running jobs?

Or if counting threads:
Code:
$ grep -i cores /proc/cpuinfo -c
8
$ grep cpu[0-99] /proc/stat -c
8

8 - 2 = 6 paralell running jobs?

EDIT:
@ gmark99
Did you try:
Code:
sleep 15
sleep 0.5

# 25  
Old 01-29-2015
Quote:
Originally Posted by sea
@ Don, might this mean counting cpus or threads?
CPU's.

To get a count, count 'core id' lines.

Code:
$ grep -c 'core id' /proc/cpuinfo

2

$

This User Gave Thanks to Corona688 For This Post:
# 26  
Old 01-29-2015
Keep in mind, once the jobs are in the background, they are running, regardless if you sleep in the foreground, so that has no effect.
Furthermore, it cant hurt the give the cpu's a second break between the jobs it executes.

Also, you could implement that check in the loop itself rather than in the background as well.
In that case, you might even be able to 'leave out' the sleep.

On the other hand, if the 'purge-job' takes several minutes, as you say, just to check if a process is running or not, you have a serious performance issue anyway.
Which could be either too many jobs, or a badly coded parser.
(EDIT2: on rethinking, i'm very sure the issue is here, since with every loop you start a background job lasting several minutes and doing db and cpu checks)

EDIT:
@ Corona, ok thank you.
So for you this would mean no background jobs then?
While i could do 6?

With any at least less modern hardware one should be able to run at least one background job, or is one not?

Last edited by sea; 01-29-2015 at 05:33 PM..
# 27  
Old 01-29-2015
Quote:
Originally Posted by sea
@ Corona, ok thank you.
So for you this would mean no background jobs then?
I'm not sure how you get "no background jobs" from "two cores". Without timesharing, two cores can run two things.

Quote:
While i could do 6?
We could both run 30 if we wanted to, they'd just have to timeshare. I'd have each core running 15 jobs at a little less than 1/15th speed. You'd have each core running 5 jobs at a little less than 1/5th speed.

For maximum efficiency, and assuming each process keeps itself busy 100% of the time, don't exceed the number of cores.

Also, there's things which running things in parallel won't help with -- I/O like deleting files, for example. Two processes deleting from one disk doesn't make it spin twice as fast.
# 28  
Old 01-29-2015
In my case, "my_job" does some database queries and decides to call a function that will result in a cross-processor message and then wait for a result to be returned.

I would still benefit from a lot of jobs in parallel if they're just waiting for results from another machine, right? This waiting for outside resources is the basic reason for my wanting to run so many jobs in background.

Am I missing anything, or am I on the right track?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why execution is different from orginal bash registry$database?

Hi all, here's my script #!/bin/ksh if then export DB_CREATE_PATH=`pwd` fi echo echo "********************--Menu--*****************************" echo "*** " echo "*** 1. Pre-Upgrade Steps "... (3 Replies)
Discussion started by: jediwannabe
3 Replies

2. Shell Programming and Scripting

How to simulate an execution queue with bash?

I'm running cygwin bash on windows 7 and I'm have some bat files that perform large builds and take a long time and a lot of memory. Therefor, I don't want to builds executing simultaneously (too much memory). How can I implement a queue so I can queue up multiple builds and only execute one... (2 Replies)
Discussion started by: siegfried
2 Replies

3. Shell Programming and Scripting

execution of a string being echoed in bash

hi all, I am trying to do a loop on a series of plotting function shown below: colorlist=(blue red green); n=0; for k in $xy; do psbasemap $range -JM$scale -B10g5 -X1 -Y1 -P -K > $outfile pscoast $range -JM$scale -B10g5 -D$res -P -W$lwidth -G$fill -O -K >> $outfile echo... (1 Reply)
Discussion started by: ida1215
1 Replies

4. Shell Programming and Scripting

Execution problems with BASH Shell Script

Hi I need help with my coding , first time I'm working with bash . What i must do is check if there is 3 .txt files if there is not 3 of them i must give an error code , if al three is there i must first arrange them in alphabetical order and then take the last word in al 3 of the .txt files... (1 Reply)
Discussion started by: linux newb
1 Replies

5. Shell Programming and Scripting

Execution Problems with bash script

Hello, can someone please help me to fix this script, I have a 2 files, one file has hostname information and second file has console information of the hosts in each line, I have written a script which actually reads each line in hostname file and should grep in the console file and paste the... (8 Replies)
Discussion started by: bobby320
8 Replies

6. Shell Programming and Scripting

Splitting file needs speedup

I've got a large file (2-4 gigs), made up of 4 columns. I'd like to split the file into two, based on the 2nd column value being even or odd. The following script does the job, but runs incredibly slow--I'm not sure it will complete this week. There must be a clever way to do this with just... (2 Replies)
Discussion started by: I.P. Freeley
2 Replies

7. Shell Programming and Scripting

Optimize and Speedup the script

Hi All, There is a script (test.sh) which is taking more CPU usage. I am attaching the script in this thread. Could anybody please help me out to optimize the script in a better way. Thanks, Gobinath (6 Replies)
Discussion started by: ntgobinath
6 Replies

8. Shell Programming and Scripting

execution time / runtime -- bash script please help!

Hello, I'm running a bash script and I'd like to get more accurate a runtime information then now. So far I've been using this method: STARTM=`date -u "+%s"` ......... *script function.... ......... STOPM=`date -u "+%s"` RUNTIMEM=`expr $STOPM - $STARTM` if (($RUNTIMEM>59)); then... (6 Replies)
Discussion started by: TehOne
6 Replies

9. Shell Programming and Scripting

Calculating delay time - bash

Hi, I am having the following problem. test > hourOfDay=06 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime 180 test > hourOfDay=07 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime 120 test > hourOfDay=08 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime bash: (9-08: value... (5 Replies)
Discussion started by: jbsimon000
5 Replies

10. Shell Programming and Scripting

bash - delay expansion of variable

Hello - I have a bash script which does some logging, and I'd like to include the line number of the echo statement that pipes into $LOGGER: MYPID=$$ MYNAME=`basename $0` LOGGER="/usr/bin/logger -t $MYNAME($LINENO) -p daemon.error" ... echo 'this is an entry into the log file' | $LOGGER ... (3 Replies)
Discussion started by: scandora
3 Replies
Login or Register to Ask a Question