Run a function in background and get its process ID
Instead of creating another script that you call from your primary script you can simply declare a function (inside your primary script or inside another file that you include), and run it in background.
#!/bin/bash myfunc() { while: do echo "I'm here" sleep 1 done } echo "Start of the script" echo "Run myfunc in background" myfunc & pid=$! echo "myfunc is executin in background with the process ID $pid" sleep 10 echo "Now we stop the process that run myfunc: kill $pid" kill $pid echo "Now we wait 10 seconds" sleep 10 echo "Now it's the end of the script"
Output:
Start of the script
Run myfunc in background
myfunc is executin in background with the process ID 812
I'm here
I'm here
I'm here
I'm here
I'm here
I'm here
I'm here
I'm here
I'm here
I'm here
Now we stop the process that run myfunc: kill 812
Now we wait 10 seconds
Now it's the end of the script
The importance of using keawork local for the declaration of local variables inside function
By default, all your declared variables are global.
Hence if you declare a variable inside a function block, this variable will be globally visible after the call of this function.
Also, if you declare a variable that is already declared global before and outside the scope of the function, actualy you modify the value of the global variable.
Here an example and its output:
#!/bin/bash BASE export HOST=nicodevlog export MAIL_FROM=contact@nicodevlog.com myfunc() { HOST=$1 MAIL_FROM=$2 echo "HOST param=$HOST et MAIL_FROM param=$MAIL_FROM" } myfunc2() { _HOST=$1 _MAIL_FROM=$2 variable_func2="coucou I've been declared in func2" echo "HOST param=$_HOST et MAIL_FROM param=$_MAIL_FROM" } myfunc3() { echo "Befin myfunc3" echo $* local HOST=$1 local MAIL_FROM=$2 local variable_func3="coucou I've been declared in func2" echo "HOST param=$HOST et MAIL_FROM param=$MAIL_FROM" } myfunc4() { variable_func4="Hello I'm visible" } myfunc "coucou1" "mailfrom1" echo "la var globale HOST=$HOST et MAIL_FROM=$MAIL_FROM" myfunc "coucou1" "mailfrom1" echo "la var globale HOST=$HOST et MAIL_FROM=$MAIL_FROM" myfunc "coucou2" "mailfrom2" echo "la var globale HOST=$HOST et MAIL_FROM=$MAIL_FROM" export HOST=nicodevlog export MAIL_FROM=contact@nicodevlog.com myfunc2 "coucou1" "mailfrom1" echo "la var globale HOST=$HOST et MAIL_FROM=$MAIL_FROM" myfunc2 "coucou2" "mailfrom2" echo "la var globale HOST=$HOST et MAIL_FROM=$MAIL_FROM" echo "variable_func2 = $variable_func2" export HOST=nicodevlog export MAIL_FROM=contact@nicodevlog.com myfunc3 "coucou1" "mailfrom1" echo "la var globale HOST=$HOST et MAIL_FROM=$MAIL_FROM" myfunc3 "coucou2" "mailfrom2" echo "la var globale HOST=$HOST et MAIL_FROM=$MAIL_FROM" echo "variable_func3 = $variable_func3" echo "before call to myfunc4 => variable_func4 = $variable_func4" myfunc4 echo "after call to myfunc4 => variable_func4 = $variable_func4"
HOST param=coucou1 et MAIL_FROM param=mailfrom1
la var globale HOST=coucou1 et MAIL_FROM=mailfrom1
HOST param=coucou1 et MAIL_FROM param=mailfrom1
la var globale HOST=coucou1 et MAIL_FROM=mailfrom1
HOST param=coucou2 et MAIL_FROM param=mailfrom2
la var globale HOST=coucou2 et MAIL_FROM=mailfrom2
HOST param=coucou1 et MAIL_FROM param=mailfrom1
la var globale HOST=nicodevlog et MAIL_FROM=contact@nicodevlog.com
HOST param=coucou2 et MAIL_FROM param=mailfrom2
la var globale HOST=nicodevlog et MAIL_FROM=contact@nicodevlog.com
variable_func2 = coucou I've been declared in func2
Befin myfunc3
coucou1 mailfrom1
HOST param=coucou1 et MAIL_FROM param=mailfrom1
la var globale HOST=nicodevlog et MAIL_FROM=contact@nicodevlog.com
Befin myfunc3
coucou2 mailfrom2
HOST param=coucou2 et MAIL_FROM param=mailfrom2
la var globale HOST=nicodevlog et MAIL_FROM=contact@nicodevlog.com
variable_func3 =
before call to myfunc4 => variable_func4 =
after call to myfunc4 => variable_func4 = Hello I'm visible
Leave a Reply