Advertisement
Guest User

Untitled

a guest
May 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.33 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. getCanonicalPath() {
  4.     local cmd_line=$1
  5.     local first=${cmd_line%% *}
  6.     local abs_path=$(which $(echo ${cmd_line%% *}))
  7.     echo ${cmd_line/$first/$abs_path}
  8. }
  9.  
  10. getNumberOfActive() {
  11.   numOfActive=0
  12.   local cmd_line="$1"
  13.   echo $(ps -C $command_line --no-headers | wc -l)
  14. }
  15.  
  16. processManager() {
  17.   canonicnaPot="$1"
  18.   while :;do
  19.     num=$(getNumberOfActive "$canonicnaPot")
  20.       if [[ $num -lt targets["$canonicnaPot"] ]]; then
  21.         $canonicnaPot&
  22.       fi
  23.     sleep interval["$canonicaPot"]
  24.   done
  25.  
  26. }
  27.  
  28. logPids() {
  29.     local cmd_path="$1"
  30.     local pids=""
  31.     for id in $(pgrep -fx "$cmd_path"); do
  32.         pids+=", $id"
  33.     done
  34.     pids=$(echo "$pids" | xargs)
  35.     echo "$pids"
  36. }
  37.  
  38. if [[ $# -ge 2 ]]; then
  39.     inputpipe="$1"
  40.     logfile="$2"
  41.     if [[ ! -p $inputpipe ]]; then
  42.         mkfifo "$inputpipe"
  43.     fi
  44.  
  45.   while [[ true ]]; do
  46.         if read line; then
  47.             assign=$(echo $line | cut -d ":" -f 1);
  48.             if [[ "$assign" == "run" ]]; then
  49.                 target_number=$(echo $line | cut -d ":" -f 2);
  50.                 check_interval=$(echo $line | cut -d ":" -f 3);
  51.                 c_line=$(echo $line | cut -d ":" -f 4);
  52.                
  53.                 canPath=$(getCanonicalPath "$c_line")
  54.                 last_cmd=$canPath
  55.                
  56.                 interval["$canPath"]="$check_interval"
  57.                 targets["$canPath"]="$target_number"
  58.                
  59.                 processManager "$canPath" &
  60.                
  61.                 controlling["$canPath"]="$!"
  62.                 echo "run ukaz";
  63.             elif [[ "$assign" == "stop" ]]; then
  64.                 command_line=$(echo $line | cut -d ":" -f 2);
  65.                
  66.                 canPath=$(getCanonicalPath "$command_line")
  67.                
  68.                 kill -9 controlling["$canPath"]
  69.                
  70.                 unset -v targets["$canPath"]
  71.                 unset -v interval["$canPath"]
  72.             elif [[ "$assign" == "exit" ]]; then
  73.                 for parent_id in ${controlling[@]}; do
  74.                     kill -9 $parent_id # Ubij starša da ne kreira več novih
  75.                     pkill -P $parent_id
  76.                 done
  77.                 rm -f "$inputpipe"
  78.                 exit 0
  79.             elif [[ "$assign" == "log" ]]; then
  80.                 date +%s%3N >> $logfile
  81.                 for canPot in "${!controlling[*]}"; do
  82.                     pids=$(logPids "${tabel1["$canPot"]}" "$canPot")
  83.                     echo "$canPot" >> $logfile
  84.                     echo "$pids" >> $logfile
  85.                 done
  86.             elif [[ "$assign" == "log last" ]]; then
  87.                 date +%s%3N >> $logfile
  88.                 echo "$last_cmd">>$logfile
  89.                 pids=$(logPids "$canPath")
  90.                 echo "$pids">> $logfile
  91.             else
  92.                 echo "Napaka: premalo argumentov!";
  93.             fi
  94.         fi
  95.   done < "$inputpipe"
  96. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement