1

I want to run a c prog in background using shell script and once the prog starts running in background my shell script should continue to run without waiting for the c prog to end.
I am using ampersand '&' for the same but it my script still waits for the c prog to end.

Please help.

This is my script.

s1.sh

. /s2.sh

func1
pid=run_mem_eater_prog
func2 $pid

s2.sh

run_mem_eater_prog() {     
     ./mem_eater&  
     curr_pid=$!
     echo $curr_pid
     return $curr_pid
}

When I run s1.sh, func1 gets executed then "run_mem_eater_prog" is called which executes ./mem_eater (c executable) and the scripts waits for it to get finished and then calls func2.

How to call func2 without waiting for mem_eater to finish?

5
  • What is func1 and func2? The pid variable in sh2.sh will be set to the string run_mem_eater_prog, but it will not call the run_mem_eater_prog shell function. Use pid=$(run_mem_eater_prog) for that.
    – Kusalananda
    Commented Mar 14, 2017 at 8:50
  • sorry for the typo. I am calling it as pid=$(run_mem_eater_prog). Commented Mar 14, 2017 at 9:56
  • In func2 I have to use the pid returned and write it into sys/fs/cgroups/memory/tasks. ./mem_eater is a c prog which does some malloc and memset in a continuous loop(50 times). Problem is, my script waits for this ./mem_eater to get finished and then runs func2, by the time $pid is passed to func2 as prog already ended i get an error of no such process exists. Commented Mar 14, 2017 at 10:06
  • It's not that the mem_eater program actually has time to finish and exit before func2 is called? Allocating memory 50 times may be a quick operation.
    – Kusalananda
    Commented Mar 14, 2017 at 10:08
  • I have added a delay of 1 sec for every loop. Commented Mar 14, 2017 at 10:14

1 Answer 1

0

Try this:

( ./mem_eater & )

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .