Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Can I run a piece of code in background instead of using another script.

[sesiv@itseelm-lx4151 ~]$ cat ./testback2
#!/bin/bash
start_time=$(date +%s)

for i in {1..5}
do
./testscript &
done
wait

        finish_time=$(date +%s)
        seconds=$((finish_time - start_time))
        hours=$((seconds / 3600))
        seconds=$((seconds % 3600))
        minutes=$((seconds / 60))
        seconds=$((seconds % 60))
        echo "Time Taken :"
        echo "$hours hour(s) $minutes minute(s) $seconds second(s)"

[sesiv@itseelm-lx4151 ~]$ ./testback2
sleeping 22436
sleeping 22438
sleeping 22440
sleeping 22442
sleeping 22435
Time Taken :
0 hour(s) 0 minute(s) 3 second(s)

i tried something like below but it gives the parent process id.I was expecting 5 different child process ids like above.But the timing here is 3 soeconds only.

#!/bin/bash
start_time=$(date +%s)
fun() {
echo "$1 $$"
sleep 3
}


for i in {1..5}
do
fun sleeping &
done
wait

        finish_time=$(date +%s)
        seconds=$((finish_time - start_time))
        hours=$((seconds / 3600))
        seconds=$((seconds % 3600))
        minutes=$((seconds / 60))
        seconds=$((seconds % 60))
        echo "Time Taken :"
        echo "$hours hour(s) $minutes minute(s) $seconds second(s)"

output :
sleeping 22028
sleeping 22028
sleeping 22028
sleeping 22028
sleeping 22028
Time Taken :
0 hour(s) 0 minute(s) 3 second(s)

NOTE:this is the testscript code

#!/bin/bash
fun() {
echo "$1 $$"
sleep 3
}

fun sleeping
share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

There are some occasions when bash creates a new process, but the old value of $$ is kept. Try $BASHPID instead.

share|improve this answer
    
BASHPID doesn't give me anything [sesiv@itseelm-lx4151 ~]$ echo "BASHPID=$BASHPID" BASHPID= –  munish Apr 26 '13 at 9:29
    
Do you use bash as an interactive shell, or something else? If yes, which version? (Type bash --version to find out.) $BASHPID was introduced in bash-4.0 –  Uwe Apr 26 '13 at 9:34
    
[user@itseelm-lx4151 ~]$ bash --version GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc. –  munish Apr 26 '13 at 9:51
    
OK, then $BASHPID doesn't work. But anyhow, if you execute something in the background, then you necessarily create a child process. The $$ variable does not always reflect that, simply because most of the time it's more convenient for a programmer if every $$ in a shell script yields the same value. –  Uwe Apr 26 '13 at 9:59
    
can this emulate bash pid [aehj@itseelm-lx4151 ~]$ cut -d " " -f 4 /proc/self/stat 25149 as mentioned here –  munish Apr 26 '13 at 10:17
show 2 more comments

I'm not sure if I understood your question, but you can use a subshell:

for i in {1..5}
do
 ( # bash code
 ) &
done

The bash code inside the () will be in the same script, but running in a subshell

share|improve this answer
add comment

$$ is the PID of the original shell process that's running the script. It is not the PID of the shell process that's doing the expansion. $$ does not change in a subshell.

If you need the PID of the subshell, a portable method is to run sh -c 'echo $PPID'. In bash ≥4, the PID of the shell process doing the expansion is in the BASHPID magic variable.

fun() {
  if [ -n "$BASHPID" ]; then
    echo "$1 $BASHPID"
  else
    echo "$1 $(sh -c 'echo $PPID')"
  fi
  sleep 3
}
share|improve this answer
add comment

I dont know if i am right but i did this to emulate bashpid for bash 3x and surprisingly results were good :

[aehj@itseelm-lx4151 ~]$ cat t
#!/bin/bash
start_time=$(date +%s)
fun() {
bashpid=`cut -d " " -f 4 /proc/self/stat`
echo "$1 $bashpid"
sleep 3
}

echo $$
for i in {1..5}
do
fun sleeping &
done
wait

        finish_time=$(date +%s)
        seconds=$((finish_time - start_time))
        hours=$((seconds / 3600))
        seconds=$((seconds % 3600))
        minutes=$((seconds / 60))
        seconds=$((seconds % 60))
        echo "Time Taken :"
        echo "$hours hour(s) $minutes minute(s) $seconds second(s)"
[aehj@itseelm-lx4151 ~]$ ./t
25578
sleeping 25580
sleeping 25583
sleeping 25586
sleeping 25589
sleeping 25592
Time Taken :
0 hour(s) 0 minute(s) 3 second(s)

another way :

[aehj@itseelm-lx4151 ~]$ cat u
#!/bin/bash
start_time=$(date +%s)

echo $$
for i in {1..5}
do
{
        fun() {
        BASHPID=`cut -d " " -f 4 /proc/self/stat`
        echo "$1 $BASHPID"
        sleep 3
        }
        fun sleeping
} &

done
wait

        finish_time=$(date +%s)
        seconds=$((finish_time - start_time))
        hours=$((seconds / 3600))
        seconds=$((seconds % 3600))
        minutes=$((seconds / 60))
        seconds=$((seconds % 60))
        echo "Time Taken :"
        echo "$hours hour(s) $minutes minute(s) $seconds second(s)"
[aehj@itseelm-lx4151 ~]$ ./u
25635
sleeping 25637
sleeping 25640
sleeping 25643
sleeping 25646
sleeping 25648
Time Taken :
0 hour(s) 0 minute(s) 4 second(s)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.