30

I would like to start a process with a nice value of -20. This requires me to use a command like

sudo nice -n -20 matlab 

However, this starts matlab as root too. Is there a way to have matlab as non-root?

My current approach is

sudo nice -n -20 sudo -u myusername matlab 

which to me looks like a hack. Is there a direct approach to do this?

6
  • You should be able to just drop the sudo. root is not needed to nice your own process. Commented Apr 18, 2013 at 19:45
  • 10
    If you want to set a higher priority than the default, you do need superuser. (-20 is the highest priority.) The only other way I can think of to do this would be to sudo renice after it is started. However, since you're running matlab interactively, that's easier said than done. Commented Apr 18, 2013 at 19:50
  • 2
    Not a hack, that's the way to go. Commented Apr 18, 2013 at 19:55
  • @jordanm - Without a sudo, this is the right command - nice -n -20 matlab and this is the output nice: cannot set niceness: Permission denied. Matlab starts up and the nice value is 0. Commented Apr 18, 2013 at 23:51
  • @AlanShutko - I could run system('sudo renice ...') in MATLAB, but matlab starts 2 processes - MATLAB and matlab_helper. I might have to do it on both. moreover I also want all my MATLAB processes to be of high priority - when I start matlabpool local for parallel processing. @HaukeLaging - I am beginning to think you are right. Commented Apr 18, 2013 at 23:55

7 Answers 7

29

I would start it normally and use "renice" afterwards...

However I was able to make a quick hack together with "su" which works:

sudo nice -n -20 su -c command_to_run user_to_run_as 

If you don't have to give sudo a password - perhaps because you've already just given it - you may add an & to put the whole thing in the background.

Since you already become root with the sudo-command, su won't ask you for a password. I was able to start a X-program from a terminal-emulator under X. If you want to run the X-program as another user than the user owning the X-session, you'll probably need to explicitly tell X to allow it (open for X-clients from that user).

3
  • 1
    Thank you. So I think the way I figured was the only way to do about. Commented Apr 19, 2013 at 2:46
  • This works! The only thing I am encountering now is that I have no standard output. How to solve this? E.g. sudo nice -n 19 su -c $(echo "test") gives no output. Commented Oct 22, 2018 at 8:46
  • My bad, you need to do sudo nice -n 19 su -c "echo 'test'"' When you want to run a function in su -c do`, see stackoverflow.com/a/3727572/2522849 Commented Oct 22, 2018 at 9:03
16

This can be done by modifying the file /etc/security/limits.conf (at least on some linux distros). In my case, I simply added:

#<domain> <type> <item> <value> my_user - nice -20 

then you can execute

nice -n -20 matlab

Logout and back in after saving changes to /etc/security/limits.conf.

This answer explains why.

2
  • 2
    After making this change, should log out/in again: unix.stackexchange.com/q/108603/247665 Commented Feb 7, 2019 at 20:48
  • Works on Raspberry Pi OS. Thank you. It's curious that the documentation in the file says the value is the maximum nice priority allowed, although it is the minimum allowed value, being the highest priority of course. Commented Sep 10 at 12:19
12

One step further @Jordan: Here's the elegant solution against sudo nice -n -xx su <username> -c matlab hack.

Note: Using username=sid, matlab meta-data dir=/var/lib/matlab, nice=-10 -- change at your will

  1. Create matlab meta-data dir
    sudo mkdir /var/lib/matlab 
  2. Add specified user to launch matlab & right permission
    sudo useradd -d /var/lib/matlab sid sudo chown sid:sid /var/lib/matlab 
  3. Set user password
    sudo passwd sid 
  4. Append following to /etc/security/limits.conf
    sid - priority -10 
  5. Setup & copy ssh-key to automate login (optional)
    ssh-keygen -t rsa #following key passwd misc ssh-copy-id sid@localhost #using sid's passwd 
  6. Create matlab shell wrapper (fix silent fail error)
    sudo -i cat &lt;&lt;EOF &gt;&gt;/usr/local/bin/wmatlab #!/bin/bash -- # A wrapper to launch matlab /usr/local/MATLAB/&lt;version&gt;/bin/matlab -desktop EOF chmod +x /usr/local/bin/wmatlab 
  7. Adjust "sid"'s login shell
    sudo usermod -s /usr/local/bin/wmatlab sid 
  8. Start matlab using ssh with Xforward
    ssh -X sid@localhost 
1
  • 3
    this should be the accepted answer. Commented Nov 11, 2017 at 23:30
2

pam allows you to set limits on nice per group its configuration file:

@grnice hard priority -20

@grnice hard nice -20

And make sure the group the process runs in grnice.

0

As @jordanm said drop sudo. You can nice your own processes to give them a lower priority:

nice -20 matlab 

No sudo.

7
  • This did not work. system('ps a -o pid -o comm -o nice') got me 13580 MATLAB 19 - MATLAB is running with the lowest priority instead of the highest. My question was on how to increase the priority and not reduce it. Commented Apr 18, 2013 at 23:47
  • 1
    Without a sudo, this is the right command - nice -n -20 matlab and this is the output nice: cannot set niceness: Permission denied. Matlab starts up and the nice value is 0. Commented Apr 18, 2013 at 23:51
  • 4
    OP wants not increase priority (negative nice), ie. "nice --20 mathlab" (double -) in the old notation, "nice -n -20 mathlab" in the new. Only root may use negative nice-values. Commented Apr 19, 2013 at 1:15
  • 1
    CAP_SYS_NICE would also allow the OP to do this without root privileges, but involves delving into Linux capabilities (something not a lot of people understand and might be more work than it's worth). I just mention it for the sake of completeness. Commented Apr 19, 2013 at 12:21
  • stackoverflow.com/questions/7635515/… Commented Apr 20, 2013 at 13:45
0

Add the user to sudoers (actually, a new file in /etc/sudoers.d, but its the same premise):

niceuser ALL=NOPASSWD:/usr/bin/nice 

Then, as the "niceuser":

niceuser@localhost $ sudo nice -n -10 command... 

and it does what I need (that is, my user can now increase the priority of {command ...}). It supports multiple users, etc. - use man 5 sudoers for details.

2
  • 5
    That still runs command as root, which is what the poster is trying to avoid. Commented Aug 12, 2016 at 20:52
  • I agree with @KyleButt - we should run the command under the same user and from the same directory, etc, same environment, etc, rather then under root which will require "setuser" or whatsoever Commented Dec 30, 2022 at 19:19
-1

Another possible option (simple, but less secure) is to enable setuid/setgid permission for nice executable.

sudo chmod +s /usr/bin/nice 

setuid assign any user who can execute the file the effective UID of the file owner's (in this case root) when executing the file, thus is equivalent as running as that user. setguid does the same for effective GID.


Or you can do it more securely:

# create a system group named `nice' groupadd -r nice # set the owner group for `nice' executable chgrp nice /usr/bin/nice # disallow other users to run `nice` chmod o-x /usr/bin/nice # allow anyone who are able to execute the file gain a setuid as root chmod u+s /usr/bin/nice # add your user to the group usermod -a -G nice <your-user-name> 

Beware you need to re-login your account for new group to take effect.

1
  • That still runs command as root, which is what the poster is trying to avoid. Commented Jul 2, 2024 at 21:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.