Reputation: 89
I want to create a shell script to run java service. The actual command is
java -Dorg.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory -Dlogback.configurationFile=conf/web/logback.xml -jar vertx-web-0.1-fat.jar -conf conf/web/conf.json -cluster -cp conf/web:conf/web/lib/slf4j-api-1.7.12.jar:conf/web/lib/logback-classic-1.1.2.jar:conf/web/lib/logback-core-1.1.2.jar
it works if I execute it just like that. But it's gonna be inaffective and messy. I decided to create a shell script :
#!/bin/sh
app_name=vertx-web-0.1-fat.jar
#::=== logger
logger_name=logback
#::=== config
config_base=conf/web
config_app=conf.json
config_log=logback.xml
#::=== arguments
args_factory_logger=org.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory
args_classpath=conf/web:conf/web/lib/slf4j-api-1.7.12.jar:conf/web/lib/logback-classic-1.1.2.jar:conf/web/lib/logback-core-1.1.2.jar
args_other=-cluster
#::=== execute
java -D$args_factory_logger -D$logger_name.configurationFile=$config_base/$config_log -jar $app_name -conf $config_base/$config_app $args_other -cp $args_classpath
But it doesn't work and the response
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
Error: Unable to access jarfile vertx-web-0.1-fat.jar
: command not found
Can you give me any clue or solution? Thank you
Regards, Stefio
Upvotes: 0
Views: 789
Reputation: 313
When you are assigning value to your variable, shell is treating special character as key character(not as value). thus trying to execute the special char ":".
What you need is to use quotes , either strong quote or week quote depending on your need.
In your case i suggest strong quotes.
so you command should be like :
args_classpath='conf/web:conf/web/lib/slf4j-api-1.7.12.jar:conf/web/lib/logback-classic-1.1.2.jar:conf/web/lib/logback-core-1.1.2.jar'
Upvotes: 0
Reputation: 1148
I think that you need to delcare var in the safe way. For best practices I allways define var like this:
myVar="My.contet"
So after a little refactoring I think that the script could be:
#!/bin/sh
app_name="vertx-web-0.1-fat.jar"
#::=== logger
logger_name="logback"
#::=== config
config_base="conf/web"
config_app="conf.json"
config_log="logback.xml"
#::=== arguments
args_factory_logger="org.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory"
args_classpath="conf/web:conf/web/lib/slf4j-api-1.7.12.jar:conf/web/lib/logback-classic-1.1.2.jar:conf/web/lib/logback-core-1.1.2.jar"
args_other="-cluster"
#::=== execute
java -D${args_factory_logger} -D$logger_name.configurationFile=$config_base/$config_log -jar $app_name -conf $config_base/$config_app ${args_other} -cp ${args_classpath}
if you want to see the command executed use the echo command:
echo "java -D${args_factory_logger} -D$logger_name.configurationFile=$config_base/$config_log -jar $app_name -conf $config_base/$config_app ${args_other} -cp ${args_classpath}"
Upvotes: 0
Reputation: 9162
To launch the java app as a service I recommend you take a look at JSVC project. This is really easy to implement and that takes care of many things for you.
Upvotes: 1