How to make faster loop in multiple directories?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make faster loop in multiple directories?
# 1  
Old 10-28-2018
How to make faster loop in multiple directories?

Hello,
I am under Ubuntu 18.04 Bionic.
I have one shell script run.sh (which is out of my topic) to run files under multiple directories and one file to control all processes running under those directories (control.sh).
I set a cronjob task to check each of them with two minutes of intervals. When a process is dead or when all processes are dead during ramp-up, each process waits for the completion of previous one. That's okay but due to time consuming process of run.sh
it keeps me waiting. To write separate scripts just dedicated to each folder does not sound logic.
Is there a modern way to do this without entering all directory names ?
Just wish say to script: "implement your code to all subfolders"

control.sh
Code:
for A in folder1 folder2 folder3 folder4 folder5 folder6 folder7 folder8 \
folder9 folder10 folder11 folder12 folder13 folder14 folder15 ; do
cd $A
PID=$(cat *.pid)
if ! [ -n "$PID" -a -e /proc/$PID ]; then
/home/boris/public_html/run.sh $A
else
    echo "process exists, exit now!"
sleep 2
fi
cd ../
done


Thanks in advance
Boris

Last edited by baris35; 10-28-2018 at 02:11 PM.. Reason: typo error fix
# 2  
Old 10-28-2018
That spec is far from being complete nor clear. Some questions (possibly among others):


- Does the "time consuming process of run.sh" finish within 8 seconds, so a 2 minute cron does make sense?
- what does "a process is dead" mean? What "ramp-up"?
- does that directory list represent the complete subdirectory tree to be operated upon, or should some subdirs be excluded (here: by not mentioning them in the list)?
- does run.sh create a PID file on entry and delete it on exit? BTW, should one single script "hang", the entire control.sh hangs.
- why the sleep for two seconds when time is critical?


Please amend / rephrase, drawing a broader (or better: complete) picture.

Last edited by RudiC; 10-30-2018 at 09:54 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-29-2018
Might I suggest that you should really use pushd and popd rather than cd in your script. If a directory in your list does not exist, your trailing cd .. will change away to somewhere you don't expect and all subsequent changes will fail.

It might be safer with something like:-
Code:
for A in folder1 folder2 folder3 folder4 folder5 folder6 folder7 folder8 \
folder9 folder10 folder11 folder12 folder13 folder14 folder15
do
   pushd $A >/dev/null 2>&1
   if [ $? -eq 0 ]
   then
      PID=$(cat *.pid)
      if ! [ -n "$PID" -a -e /proc/$PID ]
      then
         /home/boris/public_html/run.sh $A
      else
          echo "process exists, exit now!"
          sleep 2
      fi
      popd >/dev/null 2>&1
   else
      echo "Failed to change into directory $A"
   fi
done


Just a few thoughts.

Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 10-30-2018
Quote:
Originally Posted by RudiC
That spec is far from being compleate nor clear. Some questions (possibly among others):


- Does the "time consuming process of run.sh" finish within 8 seconds, so a 2 minute cron does make sense?
- what does "a process is dead" mean? What "ramp-up"?
- does that directory list represent the complete subdirectory tree to be operated upon, or should some subdirs be excluded (here: by not mentioning them in the list)?
- does run.sh create a PID file on entry and delete it on exit? BTW, should one single script "hang", the entire control.sh hangs.
- why the sleep for two seconds when time is critical?


Please amend / rephrase, drawing a broader (or better: complete) picture.
Dear Rudic,
Please find my answers below:

-Each loop takes around 20seconds
-I am trying to indicate "reboot of computer" with "dead process" and "ramp"-up" phrases
-There is no any other subdirectory under that environment
-I have just added some new lines to create pid file for each process.
-Regarding sleep 2, I have just deleted that line. My bad habits..

Just took into consideration of Robin's and Rudic's comments I have just updated both scripts as follows:

control.sh
Code:
dir="/home/boris/public_html"
cd $dir
for A in folder1 folder2 folder3 folder4 folder5 folder6 folder7 folder8 \
folder9 folder10 folder11 folder12 folder13 folder14 folder15
do
   pushd $A >/dev/null 2>&1
   if [ $? -eq 0 ]
   then
if ! ps x |grep -v grep |grep -c $(cat /$dir/$A/*.pid) >/dev/null
then
echo "$A is restarting" >> /var/log/$A_reboot.log
kill $(cat /home/boris/public_html/$A/*.pid)
rm /home/boris/public_html/$A/*
cd $dir
./run.sh $A
else
echo `date "+%d/%m/%y %R $A process was OK"` >> /var/log/testok_$A.log
fi
      popd >/dev/null 2>&1
   else
      echo "Failed to change into directory $A"
   fi
done

run.sh
Code:
venc= ........
aenc= ..........
ionice -c 3 nice -n 20 /usr/bin/ffmpeg -re -safe 0 -f concat -i 1.txt \
$venc $aenc /home/boris/public_html/$1/$1.m3u8 &
echo $! > "//home/boris/public_html/$1/$1.pid"



Thanks
Boris

Last edited by baris35; 10-30-2018 at 01:27 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make awk command faster?

I have the below command which is referring a large file and it is taking 3 hours to run. Can something be done to make this command faster. awk -F ',' '{OFS=","}{ if ($13 == "9999") print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12 }' ${NLAP_TEMP}/hist1.out|sort -T ${NLAP_TEMP} |uniq>... (13 Replies)
Discussion started by: Peu Mukherjee
13 Replies

2. Shell Programming and Scripting

Accessing multiple directories in loop

Hi Guys, I need to access multiple directories whcih is following similar structure and need to copy those files in desitination path. for eg : if ] then cd ${DIR}/Mon/loaded echo "copying files to $GRS_DIR" cp * ${DIR}/Mon/ echo "Files of Monday are Copied" fi if ] then... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

3. Shell Programming and Scripting

awk changes to make it faster

I have script like below, who is picking number from one file and and searching in another file, and printing output. Bu is is very slow to be run on huge file.can we modify it with awk #! /bin/ksh while read line1 do echo "$line1" a=`echo $line1` if then echo "$num" cat file1|nawk... (6 Replies)
Discussion started by: mirwasim
6 Replies

4. Shell Programming and Scripting

Make script faster

Hi all, In bash scripting, I use to read files: cat $file | while read line; do ... doneHowever, it's a very slow way to read file line by line. E.g. In a file that has 3 columns, and less than 400 rows, like this: I run next script: cat $line | while read line; do ## Reads each... (10 Replies)
Discussion started by: AlbertGM
10 Replies

5. Shell Programming and Scripting

Running rename command on large files and make it faster

Hi All, I have some 80,000 files in a directory which I need to rename. Below is the command which I am currently running and it seems, it is taking fore ever to run this command. This command seems too slow. Is there any way to speed up the command. I have have GNU Parallel installed on my... (6 Replies)
Discussion started by: shoaibjameel123
6 Replies

6. Shell Programming and Scripting

How to make copy work faster

I am trying to copy a folder which contains a list of C executables. It takes 2 mins for completion,where as the entire script takes only 3 more minutes for other process. Is there a way to copy the folder faster so that the performance of the script will improve? (2 Replies)
Discussion started by: prasperl
2 Replies

7. Red Hat

Re:How to make the linux pc faster

Hi, Can any one help me out in solving the problem i have a linux database server it is tooo slow that i am unable to open even the terminial is there any solution to get rid of this problem.How to make this server faster. Thanks & Regards Venky (0 Replies)
Discussion started by: venky_vemuri
0 Replies

8. Shell Programming and Scripting

awk help to make my work faster

hii everyone , i have a file in which i have line numbers.. file name is file1.txt aa bb cc "12" qw xx yy zz "23" we bb qw we "123249" jh here 12,23,123249. is the line number now according to this line numbers we have to print lines from other file named... (11 Replies)
Discussion started by: kumar_amit
11 Replies

9. Shell Programming and Scripting

Can anyone make this script run faster?

One of our servers runs Solaris 8 and does not have "ls -lh" as a valid command. I wrote the following script to make the ls output easier to read and emulate "ls -lh" functionality. The script works, but it is slow when executed on a directory that contains a large number of files. Can anyone make... (10 Replies)
Discussion started by: shew01
10 Replies

10. Solaris

looking for different debugger for Solaris or to make sunstudio faster

im using the sunstudio but it is very slow , is there ant other GUI debugger for sun Solaris or at list some ways to make it faster ? im using to debug throw telnet connection connected to remote server thanks (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question