4

I have 3 functions, like

function WatchDog {
sleep 1

#something
}
function TempControl {
sleep 480

#somthing
}
function GPUcontrol {
sleep 480

#somethimg
}

And i am runing it like

WatchDog | TempControl | GPUcontrol

This script is in rc.local file. So, logically it should run at automatically. The thing is that first function is doing fine. But second and third is not starting. But if I am starting it like

sudo bash /etc/rc.local 

that is working fine. What is the problem? The same thing if i am adding it to init.d directory.

2

Pipe sends the output of one command to the next. You are looking for the & (ampersand). This forks processes and runs them in the background. So if you ran:

WatchDog & TempControl & GPUcontrol

It should run all three simultaneously.

Also when you run sudo bash /etc/rc.local I believe that is running them in series not in parallel (it waits for each command to finish before starting the next). That would be sort of like this:

WatchDog ; TempControl ; GPUcontrol

Command Separators

; semi-colon - command1 ; command2

This will execute command2 after command1 is finished, regardless of whether or not it was successful

& ampersand - command1 & command2

This will execute command1 in a subshell and execute command2 at the same time.

|| OR logical operator - command1 || command2

This will execute command1 and then execute command2 ONLY if command1 failed

&& AND logical operator - command1 && command2

This will execute command1 and then execute command2 ONLY if command1 succeeded.

  • So the "&" waitng them to finish, and ";" runing them immediately? Cos thats the endless loops, so they should start in one time. – Zhyhalo Oleksandr Sep 22 '17 at 13:46
  • I've edited my question with more detail about command separators. – Jesse_b Sep 22 '17 at 14:02
5

Simply with GNU parallel:

export -f WatchDog && export -f TempControl && export -f GPUcontrol

parallel -j3 ::: WatchDog TempControl GPUcontrol

  • export -f <funcname> - export the function to be referred by parallel

  • -j N - run up to N jobs in parallel


Test case for demonstration:

function a () { seq -s' ' 1 10; sleep 10; }
function b () { echo {a..z}; sleep 5; }
function c () { echo {-100..-80}; sleep 10; }
export -f a && export -f b && export -f c
parallel --no-notice  -j3 ::: c b a

a b c d e f g h i j k l m n o p q r s t u v w x y z
-100 -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80
1 2 3 4 5 6 7 8 9 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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