So I created a script the the following commands
#! /usr/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/user/Desktop/Hello.jar
USER=user
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"
And I saved it in
etc/init.d/
And then ran the following command in terminal
sudo update-rc.d java_server_launch.sh defaults
I have a program located at
/home/user/Desktop/
And it is called Hello.jar and it works fine when I run it. When I restart my computer for some reason the program (Hello.jar) does not execute. What am I doing wrong?
I'm doing exactly what the answer here says.
You need to replace Hello.jar with $MY_SERVER in the last line of your bash script. That's because your current working directory isn't /home/user/Desktop
Edit: Try replacing the last line of code with this:
/bin/su $USER -c "$JAVA -jar $MY_SERVER &"
if you're running on Ubuntu you should check out upstart
see how simple it is to run jar https://stackoverflow.com/a/12102542/41576
Related
Not able to execute other instruction after if I am running a java jar file (which is present on another host) from my shell script.I tried using nohup but still not able to exit.
following is my script
#!/usr/bin/env bash
sshpass -p "${array[1]}" ssh -oStrictHostKeyChecking=no ${array[0]}#${array[2]} "cd ${array[3]} && echo -ne '\n' | nohup java -jar myapp.jar";
#some other instructions
echo "next statement"
already tried Scripts with nohup inside don't exit correctly but it doesn't worked.
sshpass -p pass ssh -o StrictHostKeyChecking=no xyz#hostname 'cd dir; nohup java -jar myapp.jar'
Try this with replacing the values appropriately
I'm trying to write a PowerShell script to update Spigot using Git Bash. Hopefully from the two failed PS examples below you get the gist of what I'm trying to do.
I can successfully open a Git Bash shell in the target folder and run java -jar BuildTools.jar. When I try to run through PowerShell, a CMD window opens and immediately closes. No errors are displayed and best I can tell, the CMD window contains no text. I prefer to use PowerShell over a CMD script because I am leveraging Invoke-WebRequest earlier on to get the latest version of BuildTools.jar. I would like to keep all this together in one script.
Example 1:
Start-Process -FilePath "C:\Program Files\Git\bin\bash.exe" -ArgumentList "--login -i -c ""java -jar BuildTools.jar"""
Example 2:
Start-Process -FilePath "C:\Program Files\Git\bin\bash.exe" -ArgumentList '--login', '-i', '-c', '"java -jar BuildTools.jar"'
Figured it out thanks to an idea from Ansgar Wiechers.
Solution is as follows:
Start-Process -FilePath "C:\Program Files\Git\bin\bash.exe" -ArgumentList '-c', '"cd /c/Bitnami/Updater && java -jar BuildTools.jar"'
I want t start a jar which should provide a Rest-WebService.
When I run following command from terminal the jar and the webservice starts successfully:
java -jar SchnittprofilService-1.0-fat.jar BH121 8888
If I run the same command in a shell script start.sh, the jar starts but the not the webservice.
The permission for start.sh is set to 777.
Any suggestions?
Oops:
You have to put your arguments in quotation marks:
java -jar SchnittprofilService-1.0-fat.jar "BH121" "8888"
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
I have a Java program Desktop/testfolder/xyz.jar on a remote machine. It has a configuration file on the same folder. When I SSH into the machine, I do:
"ssh user#remote java -cp Desktop/testfolder/xyz.jar Main"
The problem here is the configuration file is not in the path, as we are in the home folder so my program cannot read the configuration.
I want to first go into that folder and then run the program from that folder. In a shell script if I did this
"ssh user#remote cd Desktop/testfolder"
"java -cp xyz.jar Main"
it executes the first statement and when the second statement is run it runs on my current machine not the remote machine.
Can we do only one command or there are any other solutions for this?
Try something like this:
ssh you#yours.com "cd /home && ls -l"
You could try separating the commands by a semicolon:
ssh user#remote "cd Desktop/testfolder ; java -cp xyz.jar Main"
If you want to split your commands over multiple lines for the sake of readability, you could also pass the list of commands to the bash command as follows:
ssh user#remote.host bash -c "'
cd Desktop/testfolder
java -cp xyz.jar Main
'"