To run 5 commands at the same time with process from a list


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To run 5 commands at the same time with process from a list
# 1  
Old 11-06-2013
To run 5 commands at the same time with process from a list

I have many command is list in the variable lists,each command will run a very long time, so I want to run 5 commands at the same time with process till it complete run all the command,

Code:
lists="aa bb cc dd xx gg blabla zz ......." ( a very long list)

can some one point me the codes?

Lei
# 2  
Old 11-06-2013
Code:
set -- $lists # NOte $lists IS NOT QUOTED here.

while [ "$#" -gt 0 ]
do
        [ ! -z "$1" ] && command "$1" &
        [ ! -z "$2" ] && command "$2" &
        [ ! -z "$3" ] && command "$3" &
        [ ! -z "$4" ] && command "$4" &
        [ ! -z "$5" ] && command "$5" &
        shift 5
        wait
done

# 3  
Old 11-07-2013
Quote:
Originally Posted by Corona688
Code:
set -- $lists # NOte $lists IS NOT QUOTED here.

while [ "$#" -gt 0 ]
do
        [ ! -z "$1" ] && command "$1" &
        [ ! -z "$2" ] && command "$2" &
        [ ! -z "$3" ] && command "$3" &
        [ ! -z "$4" ] && command "$4" &
        [ ! -z "$5" ] && command "$5" &
        shift 5
        wait
done

Thaks it works for me, I post another thread for another issue https://www.unix.com/shell-programming-and-scripting/240129-help-about-bash-process.html , can you help me ?
# 4  
Old 11-21-2013
Quote:
Originally Posted by Corona688
Code:
set -- $lists # NOte $lists IS NOT QUOTED here.

while [ "$#" -gt 0 ]
do
        [ ! -z "$1" ] && command "$1" &
        [ ! -z "$2" ] && command "$2" &
        [ ! -z "$3" ] && command "$3" &
        [ ! -z "$4" ] && command "$4" &
        [ ! -z "$5" ] && command "$5" &
        shift 5
        wait
done



hi the shift only support the maxmum 9 in this case, how bout if I want o start 15 process one time?
# 5  
Old 11-21-2013
I seriously question the wisdom of this unless your system really does have 15 processors and 15 disks.

If you really want to run dozens like that, put it in a loop.

Code:
for ((X=0; X<15; X++))
do
        [ ! -z "$1" ] && command "$1" &
        shift 1
done

# 6  
Old 11-21-2013
If the processes have varying runtimes you might consider not waiting until all of the current 5-process-group are finished but start a new one as soon as one of the running processes have finished.

The following pseudocode should be easy to implement. FANOUT is the number of processes you want to run in parallel, TIMEOUT should be set to some sensible number of seconds:

Code:
list-of-processes
while end-of-list is not reached do
     if <nr-of-started-procs> is smaller than FANOUT
          start next_process in background
          if <get next process from list> not empty
               next_process = next list entry
          else
               exit loop
          endif
     else
          wait TIMEOUT
     endif
enddo

wait for remaining processes to finish

You might want to put your processes to start in a shell array first, so you can easily check if you have reached the last element (=end of list).

I hope this helps.

bakunin
# 7  
Old 11-21-2013
Hi.

Check to see if you have the perl-coded utility parallel on your system or in your repositories:
Quote:
GNU parallel is a shell tool for executing jobs in parallel using one or more computers. A job can be a single command or a small script that has to be run for each of the lines in the input. The typical input is a list of files, a list of hosts, a list of users, a list of URLs, or a list of tables. A job can also be a command that reads from a pipe. GNU parallel can then split the input and pipe it into commands in parallel.
http://www.gnu.org/software/parallel/

Best wishes ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to save and run a list of commands?

Dear every one, I am working with the data of my lab program and I have to do many times by the same list of some commands (grep, then save to file, then use awk to delete odd lines, save to file, use awk to delete even lines, save to file...). Is there any way to save a list of command which I... (4 Replies)
Discussion started by: phamnu
4 Replies

2. AIX

List of AIX commands that can be run by ROOT user ONLY

Hello, I am testing sudo and I want to test it. Can anyone please let me know few commands (of course other than shutdown, reboot etc. as I can't reboot the box) on AIX that can be run by ROOT only. Thanks ---------- Post updated at 07:43 PM ---------- Previous update was at 07:38 PM... (5 Replies)
Discussion started by: prvnrk
5 Replies

3. Shell Programming and Scripting

Script to run commands at a specific time.

Hello All, I have written a script which which is working fine to a certain logic of it. But i want a part of the script to run two commands at 00:10 hrs every day. These two command are 1. rm -rf /path/to/folder 2. mail the content of a file. How do i achieve this. Thanks. ... (4 Replies)
Discussion started by: Siddheshk
4 Replies

4. Shell Programming and Scripting

Create a list of commands in a central location, and then run them all remotely through a ssh here

I'm trying to write a script that in the end will from one central location hop to a bunch of servers and then run a series of ping tests. The thing is, the list of devices that needs to be pinged is going to be different for each server. So what I want to do is be able to do is read through the... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

5. UNIX for Dummies Questions & Answers

Run process with nohup every certain time

Hi, I need execute a script every 30 minutes. As might be done without using cron Thx. (6 Replies)
Discussion started by: pepeli30
6 Replies

6. Programming

kill a process which run out of time

hello everybody!! i want ur help! it is urgent!! ... pid=fork(); if(pid==0) { execl(a program); exit(1);} else if (pid>0) { timer(5); //(command 1)timer is a function that count up to 5sec if(kill(pid,0)==0)kill(pid,9);//(command 2) wait(&status); .... } else perror("error");... (3 Replies)
Discussion started by: nicos
3 Replies

7. Shell Programming and Scripting

Run several commands at a time

Hello guys, I am new at shell scripting and I want to create a script that runs several commands at a time, ie: uptime, w, df -h and so on and send the output of this commands to a text file so it can be send via email at a certain time using crontab. Any help will be much appreciated! (4 Replies)
Discussion started by: agasamapetilon
4 Replies

8. Shell Programming and Scripting

how to run multiple process at the same time

Hello guys, Look what im doing: I need to run a process from a SERVER1 to SERVER2, SERVER3 and SERVER4. The shell of the process is in each SERVER (2 to 4) So from SERVER1 i do: for i in SERVER2 SERVER3 SERVER4 do rsh $i ' ./process.sh ' done The problem is: each process.sh... (2 Replies)
Discussion started by: lestat_ecuador
2 Replies

9. Shell Programming and Scripting

how to run process in certain date and time

hi all! i want to run a process in certain date and hour (like feb 2007 ,hour 3 p.m) how shell i write it my script call cs-update-pr another question :as the script running, will i see it as process ?ho does it run background? and if not - how can i define to him to run background? thanks... (3 Replies)
Discussion started by: naamas03
3 Replies

10. Programming

Process Run time information

Hello, I am working on Sun Solaris 5.7. I am trying to read the running time of a process through a C program. One way I am reading it is by using the command ps -<pid> -f The other way is from the struct psinfo_t which is there under /proc/pid/psinfo. However, the two times are... (1 Reply)
Discussion started by: hmurali
1 Replies
Login or Register to Ask a Question