How to run jar files sequentially from a shell script - java

I am trying to run two java application one after other in my docker container.
In my dockerfile i have specified invoker.sh as the entry point.
ENTRYPOINT ["sh", "/opt/invoker.sh"]
Then i use this script to run two jar files.
#!/bin/sh
java -jar loader.jar
java -jar service.jar
but this does not work. It gives
Error: Unable to access jarfile javaimpl-loader.jar
and only the service.jar is executed. When i tried echo $(ls) it shows that both the jar files are there.
but if i change the script to
#!/bin/sh
echo $(java -jar loader.jar)
java -jar service.jar
then both the jars work. Why cant i use the 1st script. any help regarding this highly apreciated.

It appears the first example is being treated as a single line, you could work with that. Also, I would prefer bash to /bin/sh. Like,
#!/usr/bin/env bash
java -jar loader.jar && java -jar service.jar

Related

How to tell whether current bash script was invoked from call script

I have a script that ideally will be used in two different ways. One, run stand-alone from command line. Two invoke from a service script located in /etc/init.d.
I would like the script (call it run_app.sh) to work as follows:
#/bin/bash
# this is run_app.sh. It should be able to be run stand-alone or called from another script
if [ invoked by a calling script ] then
java -cp . -jar blah.jar
else
nohup java -cp . -jar blah.jar 2>&1 &
, so its the "invoked by a calling script" that I would need help with. Thank you.
If you plan to launch your script either by issuing ./run_app.sh or the service. You can just use $0:
#!/bin/bash
# this is run_app.sh. It should be able to be run stand-alone or called from another script
this_script_name="run_app.sh"
if [ "$0" == "./${this_script_name}" ] then
java -cp . -jar blah.jar
else
nohup java -cp . -jar blah.jar 2>&1 &
Instead of trying to find out how the script was called, I suggest to use a command line argument.
Script app.sh
#/bin/bash
# this is run_app.sh. It should be able to be run stand-alone or called from another script
if [ "$1" = "--service" ] then
java -cp . -jar blah.jar
else
nohup java -cp . -jar blah.jar 2>&1 &
fi
Manually run the script as app.sh, from the calling script run app.sh --service.
If you need to pass additional command line arguments to your script, you might have to implement some better option parsing.
Note: Checking $0 may work under certain conditions, but may not work in some other cases. Trying to find out details about the parent processes is even more difficult and fragile.
Another note: Your java command line arguments -cp . -jar blah.jar depend on the current directory. To make sure this works in all cases, the script should cd into the correct directory before calling java. e.g. cd $(dirname "$0") if the script is located in the same directory as blah.jar.

I want to make sh file that execute jar file directly in Ubuntu 12.04LTS

In Ubuntu 12.04 (Not Working)
I create a sh file and write the code.
#echo off
java -jar Program.jar
also allow the permission of execution.
But no action performed. While in windows it works fine.
In Windows 7 (Working)
I create a batch file and write the code.
#echo off
java -jar Program.jar
Please help what i am doing wrong.
Ubuntu shell scripts can't use #echo off that's for DOS command scripts. You need something like,
#!/bin/sh
java -jar Program.jar
The first line is known as the Shebang (the #! is a kind of magic number).

Is there a way to run more than one jarfile using -cp or -jar as just ONE command?

Is there a way to run basically: java -jar file1.jar, file2.jar (Using only that one command, not using it twice in a row?)
Or using java -cp file1.jar file2.jar (Again, using the command only ONCE to run TWO .jar files?)
(Using Cmd Prompt/Terminal/etc)
If so, how?
java -classpath <your path>\file1.jar -jar file2.jar
Idea is provide one jar as primary jar and everything else can go to class path.
You can use nohup for this:-
nohup java -jar file1.jar & && nohup java -jar file2.jar &
Explanation:-
* nohup is used to run the bash commands back ground(Ex:- nohup ls &).
* && is used to combine two bash commands into a single command but run both
the commands
* Here I am trying to combine and run two background processes.
You can see these processes running using the bash command "top" and checking appropriate process no.
Hope this helps!
If you want the processes not to run in background and if you want to see the output live use this bash Command!
java -jar file1.jar & java -jar file2.jar &
Hope this helps!!

java running jar via cronjob

I want to setup a Minecraft server, which automatically starts up on system startup.
I use the following script to manually run: /home/mc_ftb/server/start.sh
#! /bin/sh
java -Xms1G -Xmx3G -jar /home/mc_ftb/server/mcpc-plus-1.5.2-R0.2-forge716-B527.jar nogui
The cron to start it on start up looks like that:
mc_ftb Ja ~/server/start.sh
This structure already worked with other mods, like Tekkit, but now with a FTB (NewWorld) mod, I get the error
Exception in thread "main" java.lang.NullPointerException
at cpw.mods.fml.relauncher.FMLRelaunchLog.resetLoggingHandlers(FMLRelaunchLog.java:212)
at cpw.mods.fml.relauncher.FMLRelaunchLog.configureLogging(FMLRelaunchLog.java:191)
at cpw.mods.fml.relauncher.FMLRelaunchLog.log(FMLRelaunchLog.java:242)
at cpw.mods.fml.relauncher.FMLRelaunchLog.info(FMLRelaunchLog.java:274)
at cpw.mods.fml.relauncher.FMLRelauncher.setupHome(FMLRelauncher.java:164)
at cpw.mods.fml.relauncher.FMLRelauncher.relaunchServer(FMLRelauncher.java:147)
at cpw.mods.fml.relauncher.FMLRelauncher.handleServerRelaunch(FMLRelauncher.java:45)
at net.minecraft.server.MinecraftServer.main(MinecraftServer.java:1622)
at org.bukkit.craftbukkit.Main.main(Main.java:21)
Starting the script in a terminal as mc_ftb works fine, but even not with the cronjob, I already tried to start it via exce java ....
The script and the jar file are +x'ed. I'm using the actual stable Debian.
Try adding a 'source /etc/profile' as the first line in your script, this will load the environment variables first.
So replace
#! /bin/sh
java -Xms1G -Xmx3G -jar /home/mc_ftb/server/mcpc-plus-1.5.2-R0.2-forge716-B527.jar
with
#! /bin/sh
source /etc/profile
java -Xms1G -Xmx3G -jar /home/mc_ftb/server/mcpc-plus-1.5.2-R0.2-forge716-B527.jar

shell scripting: java program can't find property file when starting by sh

I am starting a java programm under Red Hat Enterprise Linux Server release 5 (Tikanga).
directory structure:
- bin ->sc.jar,start-sc.sh,sc-lib-all.jar
- conf->log4j-sc.properties,sc.properties
command to run the java programm (which is perfectly working):
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j sc.properties -jar sc.jar -config ../conf/sc.properties
if i put it into a shell script the java programm can't find the prop file anymore.
shell script (start-sc.sh) looks like:
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j-sc.properties -jar sc.jar -config ../conf/sc.properties
i am a newbie on shell scripting any ideas what i am missing? thx!
i guess you started your shell script not from the bin directory, which the dir start-sc.sh belongs to.
to explain it clear, let's make an example.
say, your script is here:
/foo/bar/bin/start-sc.sh
if you start it under /foo/bar/bin/, it (the relative path) should work.
but if you start your script from /home/yourHome/someDir/ , the relative path will point to $PWD/../, which is /home/yourHome/
you could either in your script first cd /foo/bar/bin/ before you start the java app. or do something like:
a=`dirname $0`
if [ $a = '.' ];then
a=`pwd`
fi
cd $a
/usr/java/jdkxxxx/java .....
It sound fine to me, does this version work?
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$(pwd)/../conf/log4j-sc.properties -jar sc.jar -config $(pwd)/../conf/sc.properties
Edit #1:
Try put the following before launching your program:
echo `pwd`
The output tells you where you are running your script, so you can check if it's the right path or not.
Edit #2:
Try this script
#!/bin/bash
LOG4JCONF="/absolute/path/to/the/log4j/conf/file"
SCCONF="/absolute/path/to/the/other/conf/file"
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$LOG4JCONF -jar sc.jar -config $SCCONF

Categories

Resources