tags:

views:

162

answers:

4

This one fails:

nice -n 10 exec "$JAVA" $JAVA_HEAP_MAX $NUTCH_OPTS -classpath "$CLASSPATH" $CLASS "$@"

this succeeds:

nice -n 10 java test

Does it mean can't combine nice and exec?

+1  A: 

Try:

nice -n 10 sh -c 'exec $JAVA $JAVA_HEAP_MAX $NUTCH_OPTS -classpath "$CLASSPATH" $CLASS "$@"'

exec is not an executable. I'm really not sure why you'd need to do this, though.

Matthew Flaschen
No,it reports "syntax error near unexpected token `('"
Shore
Sorry. Fixed now.
Matthew Flaschen
I've no idea why my original version doesn't work,though
Shore
+3  A: 

I'm really sure it's going to be

exec nice -n 10 "$JAVA" $JAVA_HEAP_MAX $NUTCH_OPTS -classpath "$CLASSPATH" $CLASS "$@"
Joshua
Cool!it works!But why my original version not working?
Shore
Your original version doesn't work because exec is not an executable, and nice can only run executables.
Matthew Flaschen
Why do you say exec is not an executable?Isn't exec short for "execute"?
Shore
Yes, exec is short for executable. However, exec itself is not a runnable program. There is no actual file (".exe" to speak loosely) called exec. It only makes sense inside a shell.
Matthew Flaschen
Thanks,got it:)
Shore
exec is a shell builtin that the shell recognizes. It invokes one of the exec system calls with the arguments following the exec command. Nice is an executable that calls the fork system call to create a new process which calls the exec system call with the remaining arguments. What nice doesn't do is invoke a shell in the new process and execute the command as a shell command.
D.Shawley
This answer uses the exec builtin which instructs the shell to replace itself with the nice utility. The nice utility, in turn, adjusts its priority and then uses the exec system call to launch the command ("$JAVA ...").NOTE: the previous comment was incorrect in that nice does not create a subprocess. It exec's the command in the same process.
D.Shawley
+1  A: 

Think about what it is that exec will be doing (replacing the currently running process), you can adjust the process priority separately, i.e. using top or by using renice from another process.

none
A: 

You can't directly nice exec because exec is a builtin and not a separate executable.

Nice is a program which takes in another program and runs it. It doesn't filter that program through your shell, so it will not work on shell builtins like exec.

However, there is a more fundamental problem!

Nice (the system call) is specifically problematic in that it needs to be a separate process -- since a (non-root) process can't decrease its niceness, it can't just just "increase my niceness, execute, decrease".

As such what you're trying to do would not do what you think it would do. The exec would turn the nice process into java... which is already what happens when you run nice without any exec! The existing shell would continue running.

This would be the case even if nice were a shell builtin.

If you actually want to do this, you either need to directly increase the shell's own niceness, or try to simulate the exec by nohup (or similar) the nice process and then exiting the original shell.

Captain Segfault