2

We are doing some testing and need to run a java program as a user other than root. This is on a CentOS 6.5 box. with java 8. The script calls and executes the java program. I did the following on that script without any luck.

chown user:user script
chmod 06755 script

This still runs the process as root. The following is the part of the script that calls the java program and generate the process. What would be the best way to get this to run as the user instead of root.

#SHOWCLASSES="-verbose:class"
SHOWCLASSES=

exec /opt/jdk32/bin/java $SHOWCLASSES -Xms80M -Xmx120M com.integra.linkage.ProgramDirector "$@"

When I try and run the script with this modification i get this following error

su -c "exec /opt/jdk32/bin/java $SHOWCLASSES -Xms80M -Xmx120M com.integra.linkage.ProgramDirector "$@"" -s /bin/sh esadmin

ProgramDirector: No operational mode chosen.
Usage: ProgramDirector [-wsdl programname ...]
    -wsdl       - Generate a WSDL file
    programname - The name of one or more program classes

    -mcs        - Connect to MCS and wait for messages.
Deldran
  • 153
  • 1
  • 3
  • 9
  • 1
    This isn't fit for SO because it is not programming-related. Anyways, make sure it isn't setuid root. – m0skit0 Jul 14 '15 at 14:53
  • @m0skit0 I disagree. We have `linux` tags for just such a question. In fact, I'd even say this is a dupe of a very well established question: http://stackoverflow.com/questions/6905697/how-to-run-script-as-another-user-without-password – jkeuhlen Jul 14 '15 at 14:59
  • 3
    @jkeuhlen: The referenced question was closed as off-topic, so this one should probably be closed, too. – Thomas Weller Jul 14 '15 at 15:09
  • I have tried the solution in the what you linked, and it is not working for me. Hence why I asked the question again. – Deldran Jul 14 '15 at 15:35
  • @Thomas Good point not sure how I missed that. Still confuses me why something with over 100,000 views would be off topic. – jkeuhlen Jul 14 '15 at 15:51
  • @Deldran what does "it is not working for me" mean? What happens? Are there error messages? You can try and ask this again over on the Unix&Linux SE or on Superuser, or elaborate here and I can try to help more. – jkeuhlen Jul 14 '15 at 15:52
  • Updated the OP, it looks like someone it breaks the argument when you put the command inside quotation marks. – Deldran Jul 14 '15 at 19:21
  • @jkeuhlen Views have nothing to do with it being off-topic or not. StackOverflow almost always shows at the top of Google results. – m0skit0 Jul 15 '15 at 08:31
  • @Deldran I updated my answer to try and help you more. Let me know if that works for you. – jkeuhlen Jul 15 '15 at 13:42

1 Answers1

3

As taken from how to run script as another user without password

Try using:

su -c "Your command right here" -s /bin/sh username

Just changing the ownership of a file will not cause it to be run as that user, you are just saying who can run it (root can run everything). You need to execute the command as another user.

In response to your update, let's look at why it isn't picking up the arguments you pass in:

su -c "exec /opt/jdk32/bin/java $SHOWCLASSES -Xms80M -Xmx120M com.integra.linkage.ProgramDirector "$@"" -s /bin/sh esadmin

I'm going to strip out the extra stuff to draw your attention to what matters here:

su -c "exec ... "$@"" -s /bin/sh esadmin

You have four sets of unescaped double quotes! This is most certainly going to cause some problems. Instead, so you can avoid escaping the inner quotes, simple pass in your command with single quotes and try again:

su -c 'exec /opt/jdk32/bin/java $SHOWCLASSES -Xms80M -Xmx120M com.integra.linkage.ProgramDirector "$@"' -s /bin/sh esadmin
Community
  • 1
  • 1
jkeuhlen
  • 4,401
  • 23
  • 36