Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
4  
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
1  
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

4 Answers 4

up vote 8 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

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

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

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
1  
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
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. –  Joel Davis Apr 19 '13 at 12:21
    

Your Answer

 
discard

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.