0

I am trying to kill a process by his name in a script.

Process :

toto 15408     1  0 Nov13 ?        00:20:36 java -Xmx512m -XX:MaxPermSize=128m -cp /local/toto//conf/:/local/toto//lib/* com.toto.main.entry.Launchtoto processToto

in my script :

/usr/bin/sudo -u toto /usr/bin/pkill -9 -f $pname

In sudoers :

script_user server1=(toto) NOPASSWD: /usr/bin/pkill -9 -f java *

It doesn't work. Either I have a sudo error (sudo: no tty present and no askpass program specified) or a pkill error (/usr/bin/pkill: invalid option -- 'X').

pname contains:

java -Xmx512m -XX:MaxPermSize=128m -cp /local/toto//conf/:/local/toto//lib/* com.toto.main.entry.Launchtoto processToto
tonio94
  • 341
  • 1
    What's pname set to? Please add the exact command you're running and the exact output to your question. To get the command being run, put set -x in the line about pkill in your script. – Mikel Nov 19 '15 at 16:54
  • pname contains : java -Xmx512m -XX:MaxPermSize=128m -cp /local/toto//conf/:/local/toto//lib/* com.toto.main.entry.Launchtoto processToto – tonio94 Nov 19 '15 at 16:57
  • it sounds like sudo disagrees with your intention to have NOPASSWD; are you sure that the username and servernames line up with your sudoers config? – Jeff Schaller Nov 19 '15 at 21:17

1 Answers1

1

You used a variable substitution outside of double quotes. Don't do this. Because $pname is unquoted, its value is split at whitespace characters, so pkill gets the arguments -9, -f, java, -Xmx512m, -XX:MaxPermSize=128m, -cp, /local/toto//conf/:/local/toto//lib/*, com.toto.main.entry.Launchtoto, processToto. Naturally pkill complains when it reaches the option -X which it doesn't understand.

Just pass the value of pname as an argument to pkill, i.e.

/usr/bin/sudo -u toto /usr/bin/pkill -9 -f "$pname"

As for the sudo password prompt, my crystal ball tells me you put the NOPASSWD entry before another entry without NOPASSWD for the same user. The NOPASSWD entry needs to come last. See How to run a specific program as root without a password prompt?