How to run multiple functions in Background in UNIX Shell Scripting?

Hi,

I am using ksh , i have requirement to run 4 functions in background , 4 functions call are available in a case that case is also in function, i need to execute 1st function it should run in background and return to case and next i will call 2nd function it should run in background and return to case again to ask for anyother option to execute in backgorund.

I tried to

function_name <parameters> |& 
echo $! 

using this 1 functions is being called and running and i am able to call other function but it is displaying as process already exists and the main script itself is getting terminated.

I searched the forum and saw fork(); command but i am not clear to use that and i saw in one forum that it is bad to use that and not advisable.

Please Advise how to achive the functions to run in background and i cant split the script making 4 different scripts.

Thanks,
Regards,
karthikram

function_name <parameters> &
export pid1=$!
.
.
.
wait $pid1
echo "function_name <parameters> returns $?"

Hi DGPickett/Unix tech experts,

Thanks for the idea given, the export is used for the child process to use the variable that i am able to understand, one small query i can use sleep instead of wait but whether the delay is necessary to use ? i need to the function to run in background and return to case for other options. I am currently testing the script will get back if there is any issue.

Thanks,
Regards,
Karthikram

Delay is not the point, you start all independent tasks and then dependent tasks for each are arranged to wait for all their precedents. Now, only one process can wait for each running process, so if many processed need to know what is done, a log file, directory of marker files or other database can track progress, and you can check progress in a loop every N seconds. You could write a supervisor that knows the prereqs of every new process. You could put the prereqs in a SQL db, same db as the completions, so you can query what is not run and no prereq is missing, and launch any found. You would be writing ControlM or Tivoli TWS and such.

1 Like

Hi DGPickett,

Thanks , as per your advise , i didnt use any delay instead made a condition to check whether the function pid is available which will be executed in a function which called from case (for requirment 4 is the exit made condition to check then exit).

if ps -p $<EXECUTED FUNCTION's PID> >&-  2>/dev/null ; 

Thanks All for providing your valuable idea.

Thanks,
Regards,
karthikram

Some systems even have pgrep, but if the running background task subshell leaves a marker file, that is a cheaper test. Besides, pids get reused.

1 Like

Hi DGPickett,

pgrep functionality what you have posted is available in my ksh terminal, but i am not confident about my script and its performance consistency so used the old legacy command, anyway if i get a chance will try like this (pgrep) newer commands , i hope which will be consume lesser memory.

Thanks for the gist which you gave.

Thanks,
Regards,
karthikram

  • A log file with one line per background process is nice, as you can grep or count the tightly structured records.
  • But file per process in the background in a dedicated directory is the most popular.
  • I often set processes like these in a tree of pipes, using both | and <() and occasionally >() to tie all the processes together. This ensures no latency to speak of and no polling. When the process ends, it closes stdout and that releases the reading process.
1 Like

Hi DGPickett,

I often set processes like these in a tree of pipes, using both | and <() and occasionally >() to tie all the processes together. This ensures no latency to speak of and no polling. When the process ends, it closes stdout and that releases the reading process.

Can you please provide a example of the 3rd point you referred if possible.

Thanks,
Regards,
karthikram