Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- getCanonicalPath() {
- local cmd_line=$1
- local first=${cmd_line%% *}
- local abs_path=$(which $(echo ${cmd_line%% *}))
- echo ${cmd_line/$first/$abs_path}
- }
- getNumberOfActive() {
- numOfActive=0
- local cmd_line="$1"
- echo $(ps -C $command_line --no-headers | wc -l)
- }
- processManager() {
- canonicnaPot="$1"
- while :;do
- num=$(getNumberOfActive "$canonicnaPot")
- if [[ $num -lt targets["$canonicnaPot"] ]]; then
- $canonicnaPot&
- fi
- sleep interval["$canonicaPot"]
- done
- }
- logPids() {
- local cmd_path="$1"
- local pids=""
- for id in $(pgrep -fx "$cmd_path"); do
- pids+=", $id"
- done
- pids=$(echo "$pids" | xargs)
- echo "$pids"
- }
- if [[ $# -ge 2 ]]; then
- inputpipe="$1"
- logfile="$2"
- if [[ ! -p $inputpipe ]]; then
- mkfifo "$inputpipe"
- fi
- while [[ true ]]; do
- if read line; then
- assign=$(echo $line | cut -d ":" -f 1);
- if [[ "$assign" == "run" ]]; then
- target_number=$(echo $line | cut -d ":" -f 2);
- check_interval=$(echo $line | cut -d ":" -f 3);
- c_line=$(echo $line | cut -d ":" -f 4);
- canPath=$(getCanonicalPath "$c_line")
- last_cmd=$canPath
- interval["$canPath"]="$check_interval"
- targets["$canPath"]="$target_number"
- processManager "$canPath" &
- controlling["$canPath"]="$!"
- echo "run ukaz";
- elif [[ "$assign" == "stop" ]]; then
- command_line=$(echo $line | cut -d ":" -f 2);
- canPath=$(getCanonicalPath "$command_line")
- kill -9 controlling["$canPath"]
- unset -v targets["$canPath"]
- unset -v interval["$canPath"]
- elif [[ "$assign" == "exit" ]]; then
- for parent_id in ${controlling[@]}; do
- kill -9 $parent_id # Ubij starša da ne kreira več novih
- pkill -P $parent_id
- done
- rm -f "$inputpipe"
- exit 0
- elif [[ "$assign" == "log" ]]; then
- date +%s%3N >> $logfile
- for canPot in "${!controlling[*]}"; do
- pids=$(logPids "${tabel1["$canPot"]}" "$canPot")
- echo "$canPot" >> $logfile
- echo "$pids" >> $logfile
- done
- elif [[ "$assign" == "log last" ]]; then
- date +%s%3N >> $logfile
- echo "$last_cmd">>$logfile
- pids=$(logPids "$canPath")
- echo "$pids">> $logfile
- else
- echo "Napaka: premalo argumentov!";
- fi
- fi
- done < "$inputpipe"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement