Showing posts with label shell script. Show all posts
Showing posts with label shell script. Show all posts

3/03/2023

run shell script in parallel and kill all shell when it stop

 refer to shell script



.

#!/bin/bash

# Run each script in the background
./loop_invocations.sh &
./loop_invocations.sh &
./loop_invocations.sh &
./loop_invocations.sh &

./loop_ping.sh &

# Define a function to kill all child processes
function kill_children {
echo "Stopping all child processes..."
pkill -P $$
exit 0
}

# Trap the SIGINT signal and call the function to kill child processes
trap kill_children SIGINT

# Wait for all background jobs to complete
wait

..




In this example, the kill_children function uses pkill to find and kill all child processes of the current process. The $$ variable represents the PID of the current process. The trap command sets up a handler for the SIGINT signal that calls the kill_children function. When the script is stopped with CTRL+C, the kill_children function is called, which kills all child processes before exiting the script.




Thank you.

www.marearts.com

7/03/2021

ubuntu copy all dependencies to certain folder of your executable file.

 Make copyall.sh file and make sure chmod is 775.

//

#!/bin/bash
# Author : Hemanth.HM
# Email : hemanth[dot]hm[at]gmail[dot]com
# License : GNU GPLv3
#

function useage()
{
cat << EOU
Useage: bash $0 <path to the binary> <path to copy the dependencies>
EOU
exit 1
}

#Validate the inputs
[[ $# < 2 ]] && useage

#Check if the paths are vaild
[[ ! -e $1 ]] && echo "Not a vaild input $1" && exit 1
[[ -d $2 ]] || echo "No such directory $2 creating..."&& mkdir -p "$2"

#Get the library dependencies
echo "Collecting the shared library dependencies for $1..."
deps=$(ldd $1 | awk 'BEGIN{ORS=" "}$1~/^\//{print $1}$3~/^\//{print $3}' | sed 's/,$/\n/')
echo "Copying the dependencies to $2"

#Copy the deps
for dep in $deps
do
echo "Copying $dep to $2"
cp "$dep" "$2"
done

echo "Done!"

//

And run it like this

./copy_dll.sh ./executableFileName ./copyToHere


That's all


Thank you.