nice command - Unix

This is a discussion on nice command - Unix ; Hi all, I've never used the command nice. After reading some docs I'm getting confused, so can any one help me and give an example of how to use nice? For example I've script, for which I want to reduce ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: nice command

  1. nice command

    Hi all,
    I've never used the command nice. After reading some docs I'm getting
    confused, so can any one help me and give an example of how to use
    nice?
    For example I've script, for which I want to reduce priority (let it
    5).
    I wrote this :
    nice --5 script
    I get this warning:
    nice: cannot set niceness: Permission denied

    I used chmod 777 script
    but it didn't help.

    Another question:
    how PID and PPID will change after doing nice command ?

  2. Re: nice command

    Nezhate wrote:
    > I've never used the command nice. After reading some docs I'm getting
    > confused, so can any one help me and give an example of how to use
    > nice?
    > For example I've script, for which I want to reduce priority (let it
    > 5).
    > I wrote this :
    > nice --5 script
    > I get this warning:
    > nice: cannot set niceness: Permission denied
    >
    > I used chmod 777 script
    > but it didn't help.


    Only root can use negative nice values. For non-privileged users, 0 is
    the highest priority that can be set.

    Note that there's a trap here; if you try something like:

    sudo nice --5 script

    the script will be executed with root privileges! Try this instead:

    sudo nice --5 sudo -u username script


    > Another question:
    > how PID and PPID will change after doing nice command ?


    They don't change.

  3. Re: nice command

    Nikos Chantziaras wrote:
    > Nezhate wrote:
    >> I wrote this :
    >> nice --5 script
    >> I get this warning:
    >> nice: cannot set niceness: Permission denied


    > Only root can use negative nice values. For non-privileged users, 0 is
    > the highest priority that can be set.


    And the lowest niceness number, priority being higher when niceness
    is lower, and being lower when niceness is higher.

    >> Another question:
    >> how PID and PPID will change after doing nice command ?

    >
    > They don't change.


    Well, they do change if you compare the nice-free scenario to the
    with-nice scenario. In the first, the shell is the parent of the
    command being run. In the second, the shell is the parent of the
    nice command, and the nice command is the parent of the command
    that it executes. This has no significant effect on the pid,
    since the pid of any new process is essentially a meaningless and
    opaque value (although predictable on certain implementations).
    But it does have an effect on which process is the parent of which.

    - Logan

  4. Re: nice command

    In article <483aea11$0$31737$4c368faf@roadrunner.com>,
    Logan Shaw wrote:

    >>> how PID and PPID will change after doing nice command ?

    >>
    >> They don't change.

    >
    > Well, they do change if you compare the nice-free scenario to the
    > with-nice scenario. In the first, the shell is the parent of the
    > command being run. In the second, the shell is the parent of the
    > nice command, and the nice command is the parent of the command
    > that it executes. This has no significant effect on the pid,
    > since the pid of any new process is essentially a meaningless and
    > opaque value (although predictable on certain implementations).
    > But it does have an effect on which process is the parent of which.


    $ sh -c 'echo a$$; nice -n3 perl -e "sleep 1; print \"c\$\$\n\"" & echo
    b$!'
    a16107
    b16108
    $ c16108

    a => pid of sh
    b => pid of nice
    c => pid of perl

    The 'nice' command executes 'perl' without forking.


  5. Re: nice command

    On May 27, 10:51 am, Marcel Bruinsma
    wrote:
    > In article <483aea11$0$31737$4c368...@roadrunner.com>,
    >
    > Logan Shaw wrote:
    > >>> how PID and PPID will change after doing nice command ?

    >
    > >> They don't change.

    >
    > > Well, they do change if you compare the nice-free scenario to the
    > > with-nice scenario. In the first, the shell is the parent of the
    > > command being run. In the second, the shell is the parent of the
    > > nice command, and the nice command is the parent of the command
    > > that it executes. This has no significant effect on the pid,
    > > since the pid of any new process is essentially a meaningless and
    > > opaque value (although predictable on certain implementations).
    > > But it does have an effect on which process is the parent of which.

    >
    > $ sh -c 'echo a$$; nice -n3 perl -e "sleep 1; print \"c\$\$\n\"" & echo
    > b$!'
    > a16107
    > b16108
    > $ c16108
    >
    > a => pid of sh
    > b => pid of nice
    > c => pid of perl
    >
    > The 'nice' command executes 'perl' without forking.


    I can't understand how this works?
    $ sh -c 'echo a$$; nice -n3 perl -e "sleep 1; print \"c\$\$\n\"" &
    echo
    > b$!


  6. Re: nice command

    In article
    <58f56b25-04e0-47d9-9ca2-58d83c6db018@b1g2000hsg.googlegroups.com>,
    Nezhate wrote:

    >> $ sh -c 'echo a$$; nice -n3 perl -e "sleep 1; print \"c\$\$\n\"" &
    >> echo b$!'
    >> a16107
    >> b16108
    >> $ c16108
    >>
    >> a => pid of sh
    >> b => pid of nice
    >> c => pid of perl
    >>
    >> The 'nice' command executes 'perl' without forking.

    >
    > I can't understand how this works?


    part[1]: perl -e 'sleep 1; print "c$$\n"'
    Runs the perl interpreter, with two instructions:
    sleep 1 : pause for 1 second
    print "c$$\n" : print the lowercase letter 'c' followed
    by the process ID of perl ($$ is one of
    perl's predefined variables)

    part[2]: nice -n3 perl -e 'sleep 1; print "c$$\n"'
    Runs the nice command, which lowers (-n3) its process
    priority, and then runs the perl interpreter as in [1].

    part[3]: sh -c 'echo a$$ ; sleep 5 & echo b$!'
    Runs /bin/sh, which in turn runs 'echo a$$' (print the process ID
    of sh), then runs 'sleep 5' as a separate process (the '&' tells
    sh not to wait for sleep), then runs 'echo b$!' (print the process
    ID of the previously started 'sleep 5' process).

    Replace 'sleep 5' in [3] by [2] (adjusting quotes and protecting $
    where necessary) and you get the command at the top. As shown, the
    process IDs of nice (b16108) and perl (c16108) are equal, hence
    the conclusion that the nice command does not call the fork(2)
    function before starting perl (with execvp(3) or execlp(3)).


    Regards,
    Marcel

    --
    printf -v email $(echo \ 155 141 162 143 145 154 155 141 162 \
    143 145 154 100 157 162 141 156 147 145 56 156 154 | tr \ \\)
    # O Herr, lass Hirn vom Himmel fallen! #

  7. Re: nice command

    Marcel Bruinsma wrote:
    > In article <483aea11$0$31737$4c368faf@roadrunner.com>,
    > Logan Shaw wrote:
    >
    >>>> how PID and PPID will change after doing nice command ?
    >>> They don't change.

    >> Well, they do change if you compare the nice-free scenario to the
    >> with-nice scenario. In the first, the shell is the parent of the
    >> command being run. In the second, the shell is the parent of the
    >> nice command, and the nice command is the parent of the command
    >> that it executes.


    > $ sh -c 'echo a$$; nice -n3 perl -e "sleep 1; print \"c\$\$\n\"" & echo
    > b$!'
    > a16107
    > b16108
    > $ c16108
    >
    > a => pid of sh
    > b => pid of nice
    > c => pid of perl
    >
    > The 'nice' command executes 'perl' without forking.


    Oh, good point. For some reason, I was thinking the nice command
    needed to hang around, so that it would do fork(), then exec(),
    then wait(). But it doesn't need to do so, so of course it can
    keep the same pid.

    So, you're right, and I'm wrong. :-)

    - Logan

+ Reply to Thread