Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 626  / 3 Years ago, wed, october 6, 2021, 4:25:59

Could someone help if possible. I am trying to resolve an issue I have in this script. I am getting 'else' not expected, but the logic seems to make sense.



Scripting is all new to me, but I am trying to resolve an issue with my CPU running excessively.



Error:



Do you want to set global CPU limitations y or n : y
./LIMIT A SINGLE PROCESS.sh: line 28: syntax error near unexpected token `elif'
./LIMIT A SINGLE PROCESS.sh: line 28: `elif test "$y" = "n" ; then'


Script:



#!/bin/bash
# CPU limit of a process of one application or set global limit
#
#
DAEMON_INTERVAL=3 # Daemon check interval in seconds

# gnome-terminal -x top

read -p "Do you want to set global CPU limitations y or n : " y

if test "$y" = "y" ; then

read -p "Enter Global CPU limit :" CPU_LIMIT_ALL
echo $'
All Processes shall be limited to:' $CPU_LIMIT_ALL

while true
do
PID_1="top -b -n1 -c | awk 'NR>6 && $9>CPU_LIMIT_ALL {print $1}' CPU_LIMIT_ALL=$CPU_LIMIT_ALL" # Set global CPU limit reads TOP list
NEW_PIDS=$(eval "$PID_1") # Violating PIDs
LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}')
# Already limited PIDs
QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v '^$') # PIDs in queue

for i in $QUEUE_PIDS
do
cpulimit -p "$i" -l "$CPU_LIMIT_ALL" -z & # Limit new violating processe
done

elif test "$y" = "n" ; then

read -p "Enter process to be restricted or press enter :" r
read -p "Enter value of CPU limit or press enter :" l

while true
do
echo $'
Process Entry Found'
echo $'CPU Entry Found
'

echo "Limit the Process of: $r to $l"

cpulimit --exe "$r" -b -l "$l" -z & # Set CPU limit for process
sleep 60
done

else
echo "No input found"
exit 1
fi

More From » bash

 Answers
2

I think you are missing a done before your elif in line 28. There is one done for the for loop, but no done for the while loop.


[#19558] Friday, October 8, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
utonmbo

Total Points: 134
Total Questions: 104
Total Answers: 118

Location: Argentina
Member since Mon, Jan 3, 2022
2 Years ago
;