Results 1 to 9 of 9

Thread: cpulimit - script syntax error near unexpected token `('

  1. #1

    Post cpulimit - script syntax error near unexpected token `('

    Code:
    #!/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.

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

  2. #2
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,587
    Blog Entries
    14

    Default Re: cpulimit - script syntax error near unexpected token `('

    Weird. Linux is linux, I would say. Is the script exactly the same as the one used on Ubuntu?
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  3. #3

    Default Re: cpulimit - script syntax error near unexpected token `('

    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 ?

  4. #4
    Join Date
    Jun 2008
    Location
    Earth - Denmark
    Posts
    10,730

    Default Re: cpulimit - script syntax error near unexpected token `('

    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


  5. #5
    Join Date
    Mar 2010
    Location
    Europe
    Posts
    657

    Default Re: cpulimit - script syntax error near unexpected token `('

    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
    Code:
    ~/bin/cpulimit.sh
    it works.

    If I run
    Code:
    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'.

  6. #6
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,587
    Blog Entries
    14

    Default Re: cpulimit - script syntax error near unexpected token `('

    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
    Code:
    su -c bin/cpulimit.sh 
    Wachtwoord: 
    Process 2174 detected
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  7. #7

    Default Re: cpulimit - script syntax error near unexpected token `('

    Quote Originally Posted by sled1983 View Post
    Code:
    #!/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.

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

    openSUSE 12, SUSE Enterprise 11 SP1

    Code:
    chmod 755 cpulimit.sh
    
    ./cpulimit.sh
    It works.

    Code:
    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')'

  8. #8

    Default Re: cpulimit - script syntax error near unexpected token `('

    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)

  9. #9
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    4,654

    Default Re: cpulimit - script syntax error near unexpected token `('

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •