cpulimit - script syntax error near unexpected token `('

#!/bin/bash
# ==============================================================
# CPU limit daemon - set PID's max. percentage CPU consumptions
# ==============================================================

# Variables
CPU_LIMIT=20        # Maximum percentage CPU consumption by each PID
DAEMON_INTERVAL=3   # Daemon check interval

while sleep $DAEMON_INTERVAL
do
   NEW_PIDS=$(top -b -n1 | gawk 'NR>6 && $9>CPU_LIMIT {print $1}' CPU_LIMIT=$CPU_LIMIT)         # Violating PIDs
   LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}')                               # Already limited PIDs
   QUEUE_PIDS=$(diff <(echo "$NEW_PIDS") <(echo "$LIMITED_PIDS") | grep '^<' | sed 's/< //g')   # PIDs in queue

   for i in $QUEUE_PIDS
   do
      cpulimit -p $i -l $CPU_LIMIT -z &   # Limit new violating processes
   done
done

ERROR:

sh cpulimit.sh
cpulimit_daemon.sh: command substitution: line 31: syntax error near unexpected token (' cpulimit_daemon.sh: command substitution: line 31: diff <(echo “$NEW_PIDS”) <(echo “$LIMITED_PIDS”) | grep ‘^<’ | sed ‘s/< //g’)’

On Ubuntu linux it worked.

But I used with “comm” command the script still error.


QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v "^$")   # PIDs in queue

Weird. Linux is linux, I would say. Is the script exactly the same as the one used on Ubuntu?

Knurpht wrote:
> Weird. Linux is linux, I would say. Is the script exactly the same as
> the one used on Ubuntu?

Also, how does a 20 line script have an error in line 31 ?

On 01/27/2012 12:08 PM, Dave Howorth wrote:
> Also, how does a 20 line script have an error in line 31 ?

six unseen (in html/vBulletin) EOLs, maybe??


DD
Read about openSUSE

Works for me on 11.4 and cpulimit from here: Index of /repositories/utilities/openSUSE_11.4

ETA: Ahhh … Seems like it matters how it is run:
If I run

~/bin/cpulimit.sh

it works.

If I run

sh ~/bin/cpulimit.sh

it doesn’t work. I get similar errors as shown in the OP, only it is line 15.

So drop the ‘sh’.

This is what I did, basically not much different than Lord_Elmsworth:

  • moved cpulimit.sh to ~/bin
  • chmod 755 ~/bin/cpulimit.sh
  • ran it from my homedir

Then I got this, when running it with root permissions


su -c bin/cpulimit.sh 
Wachtwoord: 
Process 2174 detected

openSUSE 12, SUSE Enterprise 11 SP1


chmod 755 cpulimit.sh

./cpulimit.sh

It works.


sh cpulimit.sh

it doesn’t work.

**ERROR: **
cpulimit_daemon.sh: command substitution: line 31: syntax error near unexpected token (' cpulimit_daemon.sh: command substitution: line 31: diff <(echo “$NEW_PIDS”) <(echo “$LIMITED_PIDS”) | grep ‘^<’ | sed ‘s/< //g’)’

sled1983 wrote:
> openSUSE 12, SUSE Enterprise 11 SP1
>
> Code:
> --------------------
>
> chmod 755 cpulimit.sh
>
> ./cpulimit.sh
>
> --------------------
>
> It works.
>
> Code:
> --------------------
>
> sh cpulimit.sh
>
> --------------------
>
> it doesn’t work.

I’ll ask the obvious question - what shell are you running in each case?
(and bear in mind that Ubuntu has different defaults)

Am 30.01.2012 12:09, schrieb Dave Howorth:
>> chmod 755 cpulimit.sh
>>
>> ./cpulimit.sh
>>
>> It works.
>>
>> sh cpulimit.sh
>>
>> it doesn’t work.
>
Calling it with sh … invokes bash in a compatibility mode with
slightly different functionality, while the script contains an explicit
#!/bin/bash not a #!/bin/sh


PC: oS 11.4 (dual boot 12.1) 64 bit | Intel Core i7-2600@3.40GHz | KDE
4.6.0 | GeForce GT 420 | 16GB Ram
Eee PC 1201n: oS 11.4 64 bit | Intel Atom 330@1.60GHz | KDE 4.8.0 |
nVidia ION | 3GB Ram