FAQ
Hi all,


  I am trying to set processor affinity for a specific process using a
shell script without result. Script:


#!/bin/sh -x


cpu_affinity_ok="2"
cpu_affinity="taskset -p -c `cat /tmp/test.pid` | awk '{print $6}'"


if [ -f /tmp/test.pid ]; then
     if [ "$cpu_affinity" == "$cpu_affinity_ok" ]; then
        exit 0
      else
         taskset -p -c 2 `cat /tmp/test.pid`
      fi
fi


This script doesn't works:


[root at cos01 bin]# taskset -p -c 27756
pid 27756's current affinity list: 2


Excecuting script:


[root at cos01 bin]# ./my_cpu_affinitty
+ cpu_affinity_ok=2
++ cat /tmp/test.pid
+ cpu_affinity='taskset -p -c 27756 | awk '\''{print }'\'''
+ '[' -f /tmp/test.pid ']'
+ '[' 'taskset -p -c 27756 | awk '\''{print }'\''' == 2 ']'
++ cat /tmp/test.pid
+ taskset -p -c 2 27756
pid 27756's current affinity list: 2
pid 27756's new affinity list: 2


  As you can see, function compare under "if" statement doesn't works ...


Any idea??


Thanks.

Search Discussions

  • Steven Tardy at Feb 26, 2014 at 12:40 pm

    On Wed, Feb 26, 2014 at 6:57 AM, C. L. Martinez wrote:


    if [ "$cpu_affinity" == "$cpu_affinity_ok" ]; then

    are you comparing strings or integers?
    # man test
            STRING1 = STRING2
                   the strings are equal
            INTEGER1 -eq INTEGER2
                   INTEGER1 is equal to INTEGER2
  • C. L. Martinez at Feb 26, 2014 at 1:28 pm

    On Wed, Feb 26, 2014 at 12:40 PM, Steven Tardy wrote:
    On Wed, Feb 26, 2014 at 6:57 AM, C. L. Martinez wrote:

    if [ "$cpu_affinity" == "$cpu_affinity_ok" ]; then
    are you comparing strings or integers?
    # man test
    STRING1 = STRING2
    the strings are equal
    INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2

    Thanks Steven, but it doesn't works also ..


    Using if [ "$cpu_affinity" -eq "$cpu_affinity_ok" ]; then
    ./cpu_affinitty: line 7: [: taskset -p -c 27756 | awk '{ print }':
    integer expression expected
  • Sjt5atra at Feb 26, 2014 at 1:40 pm

    On Feb 26, 2014, at 8:28 AM, "C. L. Martinez" wrote:
    On Wed, Feb 26, 2014 at 12:40 PM, Steven Tardy wrote:
    On Wed, Feb 26, 2014 at 6:57 AM, C. L. Martinez wrote:

    if [ "$cpu_affinity" == "$cpu_affinity_ok" ]; then
    are you comparing strings or integers?
    # man test
    STRING1 = STRING2
    the strings are equal
    INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2
    Thanks Steven, but it doesn't works also ..

    Using if [ "$cpu_affinity" -eq "$cpu_affinity_ok" ]; then
    ./cpu_affinitty: line 7: [: taskset -p -c 27756 | awk '{ print }':
    integer expression expected

    Yes, since you are double quoting you are using strings. Try using a single = sign instead of your original double equal sign.
  • C. L. Martinez at Feb 26, 2014 at 1:45 pm

    On Wed, Feb 26, 2014 at 1:40 PM, sjt5atra wrote:


    On Feb 26, 2014, at 8:28 AM, "C. L. Martinez" wrote:

    On Wed, Feb 26, 2014 at 12:40 PM, Steven Tardy wrote:
    On Wed, Feb 26, 2014 at 6:57 AM, C. L. Martinez wrote:

    if [ "$cpu_affinity" == "$cpu_affinity_ok" ]; then
    are you comparing strings or integers?
    # man test
    STRING1 = STRING2
    the strings are equal
    INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2
    Thanks Steven, but it doesn't works also ..

    Using if [ "$cpu_affinity" -eq "$cpu_affinity_ok" ]; then
    ./cpu_affinitty: line 7: [: taskset -p -c 27756 | awk '{ print }':
    integer expression expected
    Yes, since you are double quoting you are using strings. Try using a single = sign instead of your original double equal sign.



    Ok, problem solved. With this compare function:


    if [[ "$bro_cpu_affinity" == *"$cpu_affinity_ok"* ]]; then


    works ok ...


    sjt5atra, using a single =, it doesn't works ...
  • Tris Hoar at Feb 26, 2014 at 1:54 pm

    On 26/02/2014 13:45, C. L. Martinez wrote:
    On Wed, Feb 26, 2014 at 1:40 PM, sjt5atra wrote:



    On Feb 26, 2014, at 8:28 AM, "C. L. Martinez" wrote:

    On Wed, Feb 26, 2014 at 12:40 PM, Steven Tardy wrote:
    On Wed, Feb 26, 2014 at 6:57 AM, C. L. Martinez wrote:

    if [ "$cpu_affinity" == "$cpu_affinity_ok" ]; then
    are you comparing strings or integers?
    # man test
    STRING1 = STRING2
    the strings are equal
    INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2
    Thanks Steven, but it doesn't works also ..

    Using if [ "$cpu_affinity" -eq "$cpu_affinity_ok" ]; then
    ./cpu_affinitty: line 7: [: taskset -p -c 27756 | awk '{ print }':
    integer expression expected
    Yes, since you are double quoting you are using strings. Try using a single = sign instead of your original double equal sign.

    Ok, problem solved. With this compare function:

    if [[ "$bro_cpu_affinity" == *"$cpu_affinity_ok"* ]]; then

    works ok ...

    sjt5atra, using a single =, it doesn't works ...

    The issues are to do with your variable expansion


    [root at srvman ~]# cpu_affinity="taskset -p -c `cat /var/run/crond.pid` |
    awk '{print $6}'"
    [root at srvman ~]# echo $cpu_affinity
    taskset -p -c 2532 | awk '{print }'


    I think your script is still broken, as you are now just looking for any
    number matching $cpu_affinity_ok in $cpu_affinity. You should be able to
    do an integer comparison for your if statement.


    Tris




    *************************************************************
    This email and any files transmitted with it are confidential
    and intended solely for the use of the individual or entity
    to whom they are addressed. If you have received this email
    in error please notify postmaster at bgfl.org


    The views expressed within this email are those of the
    individual, and not necessarily those of the organisation
    *************************************************************
  • C. L. Martinez at Feb 26, 2014 at 2:16 pm

    On Wed, Feb 26, 2014 at 1:54 PM, Tris Hoar wrote:
    On 26/02/2014 13:45, C. L. Martinez wrote:
    On Wed, Feb 26, 2014 at 1:40 PM, sjt5atra wrote:



    On Feb 26, 2014, at 8:28 AM, "C. L. Martinez" wrote:

    On Wed, Feb 26, 2014 at 12:40 PM, Steven Tardy wrote:
    On Wed, Feb 26, 2014 at 6:57 AM, C. L. Martinez wrote:

    if [ "$cpu_affinity" == "$cpu_affinity_ok" ]; then
    are you comparing strings or integers?
    # man test
    STRING1 = STRING2
    the strings are equal
    INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2
    Thanks Steven, but it doesn't works also ..

    Using if [ "$cpu_affinity" -eq "$cpu_affinity_ok" ]; then
    ./cpu_affinitty: line 7: [: taskset -p -c 27756 | awk '{ print }':
    integer expression expected
    Yes, since you are double quoting you are using strings. Try using a single = sign instead of your original double equal sign.

    Ok, problem solved. With this compare function:

    if [[ "$bro_cpu_affinity" == *"$cpu_affinity_ok"* ]]; then

    works ok ...

    sjt5atra, using a single =, it doesn't works ...
    The issues are to do with your variable expansion

    [root at srvman ~]# cpu_affinity="taskset -p -c `cat /var/run/crond.pid` |
    awk '{print $6}'"
    [root at srvman ~]# echo $cpu_affinity
    taskset -p -c 2532 | awk '{print }'

    I think your script is still broken, as you are now just looking for any
    number matching $cpu_affinity_ok in $cpu_affinity. You should be able to
    do an integer comparison for your if statement.

    Tris

    Uhmm .. You are right Tris ... The correct option is what John Doe says ..


    Many thanks to all.
  • John Doe at Feb 26, 2014 at 1:51 pm
    From: C. L. Martinez <carlopmart@gmail.com>

    I am trying to set processor affinity for a specific process using a
    shell script without result. Script:

    #!/bin/sh -x

    cpu_affinity_ok="2"
    cpu_affinity="taskset -p -c `cat /tmp/test.pid` | awk '{print
    $6}'"

    if [ -f /tmp/test.pid ]; then
    ? ? if [ "$cpu_affinity" == "$cpu_affinity_ok" ]; then
    ? ? ? exit 0
    ? ? else
    ? ? ? ? taskset -p -c 2 `cat /tmp/test.pid`
    ? ? fi
    fi

    This script doesn't works:

    As you can see, function compare under "if" statement doesn't
    works ...

    This works for me:


    function cpu_affinity() {
    ? taskset -p -c $1 | awk ' { print $6 } '
    }


    and


    PID=`cat /tmp/test.pid`
    if [ $(cpu_affinity $PID) -eq $cpu_affinity_ok ]; then


    JD

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcentos @
categoriescentos
postedFeb 26, '14 at 11:57a
activeFeb 26, '14 at 2:16p
posts8
users4
websitecentos.org
irc#centos

People

Translate

site design / logo © 2021 Grokbase