Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
You should be able to just drop the sudo. root is not needed to nice your own process. – jordanm Apr 18 '13 at 19:45
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. – Alan Shutko Apr 18 '13 at 19:50
Not a hack, that's the way to go. – Hauke Laging Apr 18 '13 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. – Lord Loh. Apr 18 '13 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. – Lord Loh. Apr 18 '13 at 23:55
up vote 12 down vote accepted

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).

share|improve this answer
Thank you. So I think the way I figured was the only way to do about. – Lord Loh. Apr 19 '13 at 2:46

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(PERPARE)

    sudo mkdir /var/lib/matlab

  2. Add specified user to launch matlab & right persimisson

    sudo useradd -d /var/lib/matlab sid
    sudo chown sid:sid /var/lib/matlab
  1. Set user(sid) password

    sudo passwd sid

  2. Append following to /etc/security/limits.conf

    sid - priority -10

  3. 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
  1. Create matlab shell wrapper(fix silent fail ERR)
    sudo -i
    cat >/usr/local/bin/wmatlab
    #!/bin/bash --
    # A wrapper to launch matlab
    /usr/local/MATLAB/<version>/bin/matlab -desktop
    chmod +x /usr/local/bin/wmatlab
  1. Ajust sid's login shell

    sudo usermod -s /usr/local/bin/wmatlab sid

  2. start matlab using ssh with Xforward

    ssh -X sid@localhost

share|improve this answer

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.

share|improve this answer

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

nice -20 matlab

No sudo.

share|improve this answer
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. – Lord Loh. Apr 18 '13 at 23:47
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. – Lord Loh. Apr 18 '13 at 23:51
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. – Baard Kopperud Apr 19 '13 at 1:15
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. – Bratchley Apr 19 '13 at 12:21

Relatively new feature to the kernel, but if your platform supports it you can create a cgroup for the pid after the fact give it the PID of the process and manipulate its cpu.shares value to give it as much as or as little as it needs. more info

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.