# Executing statements inside forloop parallely

**URL:** <https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020>\
**Category:** UNIX for Beginners Q & A\
**Created:** [April 29, 2018, 9:41am UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020 "2018-04-29T09:41:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamesh\_G](https://community.unix.com/letter_avatar/kamesh_g/32/6_449c533f586939b6e5f246632a46421f.png) [@Kamesh\_G](https://community.unix.com/u/Kamesh_G)\
**Post date:** [April 29, 2018, 9:41am UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/1 "2018-04-29T09:41:54Z")

</div>

Hi All,

I have made a forloop as given below.

```nohighlight
if [[-f "serv_AAA_DS.ldif" && -s "serv_AAA_DS.ldif"]]; then 
    echo -e "serv_AAA_DS.ldif file created and will be splitted into 8 smaller files for ldapadd \n"
    sh ./file_splitter.sh
    for (( w=1; w <= 8; ++w ))
    do 
    	nohup ldapadd -x -c -h PL0 -p 389 -D "$LDAP_USER" -w $LDAP_PWD -f serv_AAA_DS.ldif_$w > serv_AAA_DS.ldif_$w.log
    done 
    wait
    serv_AAA_DS_count=`cat serv_AAA_DS.ldif | grep '^dn: ' | wc -l`
    echo -e "Count of serv AAA entries present in the Input file is $serv_AAA_DS_count, cross-check the value with total_dm.sh counters & also the check the failures if any in serv_AAA_DS.ldif_*.log \n"
else 
    echo -e "serv_AAA_DS.ldif is empty and will be removed \n"
    rm serv_AAA_DS.ldif
fi

```

This one runs sequentially waiting for each iteration to finish inside the forloop, I want to execute all the 8 ldapadd parallel and then the code should wait for all the 8 threads to complete, before it moves to the next lines.

Can you let me know what changes to be done on this to achieve the above mentioned.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [April 29, 2018, 10:09am UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/2 "2018-04-29T10:09:06Z")

</div>

Assuming your code works correctly, I cannot verify that at the moment:

```nohighlight
nohup ldapadd -x -c -h PL0 -p 389 -D "$LDAP_USER" -w $LDAP_PWD -f serv_AAA_DS.ldif_$w > serv_AAA_DS.ldif_$w.log &

```

Add an ampersand (& red above) at the end of the line. This works for bash which seems to be the shell you use for this code. If it is another different shell please let us know.

Note:

```nohighlight
> serv_AAA_DS.ldif_$w.log

```

will overwrite the log file every time the script runs, so you lose the content of previous logs. That may or may not be okay.

---

<div class="post-metadata">

**Author:** ![Kamesh\_G](https://community.unix.com/letter_avatar/kamesh_g/32/6_449c533f586939b6e5f246632a46421f.png) [@Kamesh\_G](https://community.unix.com/u/Kamesh_G)\
**Post date:** [April 29, 2018, 12:09pm UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/3 "2018-04-29T12:09:36Z")

</div>

@jim mcnamara - One more query, I have a script.sh that waits for user input to begin. i.e. it has "read" in the script. Now im not able to log the script.sh out as when i do ./script.sh \> out.log it doesnt prompt for the input. Also the preferrable mode is to run it as "nohup ./script.sh &" . Can you help me how to log all the outputs from the script ????

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [April 29, 2018, 4:48pm UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/4 "2018-04-29T16:48:27Z")

</div>

> [@kamesh g](#):
>
> Also the preferrable mode is to run it as "nohup ./script.sh &" . Can you help me how to log all the outputs from the script ????

The real question is not where the output goes (when you start a process with `nohup` per default its output goes to `./nohup.out` ) but where the input is supposed to come from.

When you start a process on the commandline this commandline is displayed in a terminal. Per default input comes from this terminal (the keyboard) and output goes to this terminal (the screen). Starting a process with `nohup` cuts this connection to a terminal - actually this is the whole point of `nohup` : allowing a process to continue even if its terminal is closed ("hangs up").

Bottom line: if you want to start the process with `nohup` you need something which replaces the terminal as a source of input. That can be some file, a named pipe or whatever - but something has to be there and you need to use redirection to make the process use this instead of waiting indefinitely.

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![Kamesh\_G](https://community.unix.com/letter_avatar/kamesh_g/32/6_449c533f586939b6e5f246632a46421f.png) [@Kamesh\_G](https://community.unix.com/u/Kamesh_G)\
**Post date:** [April 29, 2018, 11:45pm UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/5 "2018-04-29T23:45:49Z")

</div>

@bakunin - thank you for the help. one more query while the script runs. I'm getting the below

script:

```nohighlight
    for (( w=1; w <= 8; ++w ))
    do 
    	nohup ldapadd -x -c -h PL0 -p 389 -D "$LDAP_USER" -w $LDAP_PWD -f serv_AAA_DS.ldif_$w > serv_AAA_DS.ldif_$w.log &
    done 
    wait

```

To be suppressed:-

```nohighlight
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout

```

Is there a way to suppress this.???

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [April 30, 2018, 3:16am UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/6 "2018-04-30T03:16:57Z")

</div>

```nohighlight
nohup: redirecting stderr to stdout

```

You get that message as UNIX is known to be quite verbous, to say :  
As you havent defined anything special for stderr, it will redirect stderr to stdout which is normally your screen...  
I would not suggest to redirect to /dev/null as you are in developing stage, you need to be able to read what errors you may get so the best is to redirect in a file, a separate one, or in your log  
So your command line could be

```nohighlight
nohup ldapadd -x -c -h PL0 -p 389 -D "$LDAP_USER" -w $LDAP_PWD -f serv_AAA_DS.ldif_$w \
> serv_AAA_DS.ldif_$w.log 2>&1 &

```

---

<div class="post-metadata">

**Author:** ![Kamesh\_G](https://community.unix.com/letter_avatar/kamesh_g/32/6_449c533f586939b6e5f246632a46421f.png) [@Kamesh\_G](https://community.unix.com/u/Kamesh_G)\
**Post date:** [April 30, 2018, 10:19am UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/7 "2018-04-30T10:19:11Z")

</div>

Dears,

Please let me know what is the way to print the time taken to complete execution of the below script.

The below will run 8 parallel threads, would like to print the total time taken for all the threads to complete the execution.

```nohighlight
    for (( w=1; w <= 8; ++w ))
    do 
    	nohup ldapadd -x -c -h PL0 -p 389 -D "$LDAP_USER" -w $LDAP_PWD -f serv_AAA_DS.ldif_$w > serv_AAA_DS.ldif_$w.log &
    done 
    wait

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [April 30, 2018, 10:21am UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/8 "2018-04-30T10:21:48Z")

</div>

do `date +%s` before the loop and after `wait` - do the math.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [April 30, 2018, 10:53am UTC](https://community.unix.com/t/executing-statements-inside-forloop-parallely/372020/9 "2018-04-30T10:53:17Z")

</div>

Given your shell - which you fail to mention, BTW - is `bash` , and you don't need more than second accuracy, you can save the `SECONDS` variable's contents before and after the loop and then calculate the difference. `man bash` :

> [@](#):
>
> SECONDS  
> Each time this parameter is referenced, the number of seconds since shell invocation is returned.
