Deldran
Deldran

Reputation: 153

Run a specific java program as a different user

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.

Upvotes: 2

Views: 11346

Answers (1)

jkeuhlen
jkeuhlen

Reputation: 4517

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

Upvotes: 3

Related Questions