1

I have a java program that runs as a service in linux box. I have shell script file that has the following line to start the program.

$EXEC -home "$JAVA_HOME" -cp "$CLASS_PATH" -outfile "$LOG_OUT" -errfile "$LOG_ERR" -pidfile "$PID" $1 $CLASS 

$CLASS_PATH has class path
$CLASS has the name of main class
EXEC="/usr/bin/jsvc"

I can start and stop the service using following commands

  • service myscriptfilename start

  • service myscriptfilename stop

Now I added a new argument to my program called "myflag" . It works fine on windows box . Now I am having difficulty passing the new argument to my program on my linux box using the shell script.

Now I am starting my service as

  • service myscriptfilename start myflag

I can get the value of myflag using $2 in shell script. I am trying to figure out how do I pass that to my program

How can i pass my "myflag" to my program from shell script in the following line?

$EXEC -home "$JAVA_HOME" -cp "$CLASS_PATH" -outfile "$LOG_OUT" -errfile "$LOG_ERR" -pidfile "$PID" $1 $CLASS
Kenner Dev
  • 327
  • 1
  • 4
  • 13
  • Are you asking [this](http://stackoverflow.com/questions/9057387/process-all-arguments-except-the-first-one)? – user3707125 Nov 22 '15 at 00:48
  • I can get the value of myflag using $2 in shell scriipt . I am trying to figure out how do I pass that to my program? – Kenner Dev Nov 22 '15 at 00:58
  • Who knows how your program parses command line arguments. There's no agreed standard, so there could be a million ways. It's *your* program after all, and you should know better than us... (You didn't even say what's `$EXEC`, for that matter; maybe it's known to all Java programmers? I'm not sure.) – 4ae1e1 Nov 22 '15 at 01:04
  • I am looking for args[0].It is working fine in windows box . I am not sure where to pass that argument in linux – Kenner Dev Nov 22 '15 at 01:05

1 Answers1

1

I am considering that $EXEC is java executable, $1 is your JAR, $CLASS is your main class. In this case just append ${@:2} to the end of the line:

$EXEC -home "$JAVA_HOME" -cp "$CLASS_PATH" -outfile "$LOG_OUT" -errfile "$LOG_ERR" -pidfile "$PID" $1 $CLASS ${@:2}
user3707125
  • 3,394
  • 14
  • 23
  • EXEC="/usr/bin/jsvc" and $1=start from the command service myscriptfilename start myflag – Kenner Dev Nov 22 '15 at 01:23
  • Should work nevertheless - it has similar [execution options](http://commons.apache.org/proper/commons-daemon/jsvc.html). – user3707125 Nov 22 '15 at 01:25
  • @KennerDev, can you investigate which part doesn't work? Have you tried replacing `${@:2}` with `myflag`? There are two places where your parameter may not be passed: `service start` and `jsvc ...`. Try to determine which of these doesn't work. – user3707125 Nov 22 '15 at 03:11
  • It is at jsvc level .It is passing at service start. – Kenner Dev Nov 22 '15 at 03:45
  • @KennerDev, are you retrieving them through [DaemonContext](https://commons.apache.org/proper/commons-daemon/apidocs/org/apache/commons/daemon/DaemonContext.html#getArguments%28%29) ? – user3707125 Nov 22 '15 at 03:50
  • I am using args[0].toString().trim(); I tried hard coding the value of myflag but it is still not getting passed. – Kenner Dev Nov 22 '15 at 03:59