Writing a bash or some other script to restart another script - java

I am running a minecraft server for my son, i'm new at minecraft servers, anyway it seems it keeps dying as it cant keep up? ok so its a VM and i will be assigning more resources to it at some point but my question is as follows
I initiate the server to load up via a script
bukkit.sh is the script an inside it has
#!/bin/bash
java -Xmx1024M -jar /minecraftserver/bukkitserver/craftbukkit-1.7.1.jar -o true
now at somepoint during the day or night it will die as it cant keep up, is there a way i can have some other script run alongside it and see that the process has died and to run that script again to start the server.
when i run top i can see that java is at the top using all the resources so im 100% thats the minecraft java. Does the PID stay the same each time it loads up?
Would be great if someone could let a hand on this ...

I assume the java process remains in the foreground once it's launched? Is there any reason you can't just do:
while :
do
java -Xmx1024M -jar /minecraftserver/bukkitserver/craftbukkit-1.7.1.jar -o true
done
Then any time the java exits, the script will simply restart it.

EDIT:
You could create a script, lets call it craftbukkit.sh
Make sure it is execute by using: chmod +x craftbukkit.sh
Then inside the script you would write:
#!/bin/sh
ps auxw | grep craftbukkit-1.7.1.jar| grep -v grep > /dev/null
if [ $? != 0 ]
then
/path/to/your/bukkit.sh
fi
Change /path/to/your/bukkit.sh to wherever that minecraft startup script is.
Then you need to add this script to your crontab, this is accomplished by entering the crontab editor: crontab -e
If you want the script to run every 5 minutes, add a line that looks like this:
*/5 * * * * /path/to/craftbukkit.sh
Note that you must change the /path/to to point to where you have created the craftbukkit.sh script.

Related

A bat file to execute multiple commands, the previous command to complete, to execute the next command, how to write?

A bat file to execute multiple commands, the previous command to complete, to execute the next command, how to write?
The contents of the batch file are as follows:
java -jar module_eureka-1.0-SNAPSHOT.jar
java -jar module_config-1.0-SNAPSHOT.jar
java -jar module_gateway-1.0-SNAPSHOT.jar
java -jar module_uaa-1.0-SNAPSHOT.jar
java -jar module_user-1.0-SNAPSHOT.jar
java -jar module_news-1.0-SNAPSHOT.jar
If these are web processes, then you will need some way to know if the server is up and running. You could build out a health check api and use that to know if you should try and start the next server
java -jar module_eureka-1.0-SNAPSHOT.jar & \ # start server in background
sleep 15 && \ # wait for it to start
curl -I http://localhost:8080/api/health && # check if server is up
java -jar module_config-1.0-SNAPSHOT.jar & # start next server in background
... etc.
This is not great logic, because how do you know that the health check will always be good or bad after 15 seconds? You should probably just hit the health check api and keep retrying for a given amount of time

How to install Java application to my linux system

I have written a Java application that analyzes my phone bills and calculates the average. At the moment I execute it like:
$ java -jar analyze.jar bill_1.pdf bill_2.pdf
But I want to install the application to my system. So I can just fire up a terminal type in the name of the application and the arguments and hit enter. Like any other "normal" program.
$ analyze bill_1.pdf bill_2.pdf bill_3.pdf
I know I can write a shell script and install it to "/usr/bin/" but I can't believe that there is no "native" way.
So please help, sorry for dump question.
Thank's in advance
One neat little trick is that you can append a basic shell script to the start of the jar file which will run it appropriately. See here for the full example but the basics are:
stub.sh
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar $MYSELF "$#"
exit 1
Then do...
cat stub.sh helloworld.jar > hello.run && chmod +x helloworld.run
And you should be all set! Now you can just call the script-ified jar directly.
./helloworld.run
What you did so far is basically "the native" way.
You have to keep in mind: Java applications are compiled to byte code. There simply is no binary for your application that you could invoke. You do need this detour of calling some JVM installation with a pointer to the main class you want to run. In owhther words; that is what the vast majority of java applications are doing.
Theoretically, there are products out that there that you could use to actually create a "true" binary from your application; but that isn't an easy path (see here for first starters); and given your statement that your just looking for more "convenience" it is definitely inappropriate here.

How to update bash script whilst running or easily restart it?

I require a method to easily restart/replace bash scripts without manually stopping/starting the process over again.
Here's how my current script works:
#!/bin/sh
while true
do
echo "1"
java -server -Xms2G -jar game.jar nogui
done
Basically, if the game stops, it will automatically restart however, the script is already loaded into memory so if I was to modify it, I would have to manually kill the script and start it up again in order for the changes to take effect. I'm running at least 200 instances of the game server itself so, it would not be intelligent to do this manually and kill, then start each script manually again.
When the game server stops, I wish for it to update the script, here is an example of what I may want to do:
#!/bin/sh
while true
do
echo "1"
echo "2"
java -server -Xms2G -jar game.jar nogui
done
However, I'd need to physically stop the script and start it again myself in order for it to add the new 'echo "2"', I need an easier way to replace the running script.
The script itself will download the updated script when the game stops (so it will automatically start again.
How can I make the script unload itself from memory, and use the new script?
If I cannot do this, is there any alternative method you can suggest?
The alternative method is to use a process supervision tool such as runit, daemontools, upstart, supervisord, systemd, etc.
For any of these, you would write only the following script:
#!/bin/sh
exec java -server -Xms2G -jar game.jar nogui
...and the supervision tool would be responsible for restarting the server on exit, rereading the script (and thus responding to updates) each time it needs to be run.
These tools give you far more features than just reliable restarts -- they typically also manage logging, staged shutdown (ie. TERM + timeout + KILL), custom cleanup commands, signal hooks, etc.
See also the ProcessManagement page on the freenode #bash wiki.
If you can't use a process management system (and you really, really should!), you can have the script exec itself. That is to say:
#!/bin/sh
# ...do stuff here...
exec "$0" "$#" # and restart
Note that this is unreliable for various reasons related to BashFAQ #28.
Call the following file myscript.sh. It reloads itself into memory every time that the game stops:
#!/bin/sh
java -server -Xms2G -jar game.jar nogui
exec /path/to/myscript.sh

Calling a script from Java

I have a java program that uses the JSch libraries. I can successfully call scripts from it, but when calling one script I have that calls another script, it doesn't end up calling the other one. I have tested it in command line and it works just fine. I know I am calling the script correctly from Java because part of it executes. The script is below.
MOVIE_ROOT="/path/to/Movies"
IMDB_SCRIPT="/path/to/imdb-lookup/imdb-mf.sh"
MOVIE_FOLDER="$1"
MOVIE_FILE="$2"
MOVIE_NAME=${MOVIE_FILE%.*};
# If there isn't any info for the movie
if [ ! -f "$MOVIE_ROOT/$MOVIE_FOLDER/$MOVIE_NAME.jpginfo" ] ; then
cd "$MOVIE_ROOT/$MOVIE_FOLDER"; $IMDB_SCRIPT -p -t "\"$MOVIE_NAME\"" > "$MOVIE_ROOT/$MOVIE_FOLDER/$MOVIE_NAME.mvinfo";
exit 0;
fi
cat "$MOVIE_ROOT/$MOVIE_FOLDER/$MOVIE_NAME.mvinfo";
exit 0;
I also know that imdb-mf.sh works as well. I have echod out the line cd "$MOVIE_ROOT... and pasted that into command line and it works fine. The only time it doesn't work is when I run the script from Java. All the scripts have correct permissions set and are chmod +x. Any ideas on what's going wrong?

Running a jar file (within a loop) in a script with redirected output

I'm trying to run a jar file multiple times (in a loop) and redirect its output to a file (using the append >> operator). I'm running, and must keep using, Windows 7. I tried doing this in a windows batch file and ran into the problem below so I installed Cygwin to make use of the bash script. My script is below:
for i in {1..10..1}
do
echo "Run $i"
java -jar myjar.jar -cl >> runresults.txt
echo "Sleeping..."
sleep 60
echo "Awake!"
done
The problem: The script (Windows batch or Cygwin bash) runs only some iterations before hanging up (usually never more than 3). There's no error given. I added the sleep command to ensure that the prior iteration had time to release any locks prior to any attempt to make the next run. I've increased the sleep time to 200+ seconds and the behavior is still the same. Can anyone help me out on this?
if [ ! -f runresults.txt ]; then
touch runresults.txt
fi
for i in {1..10}
do
echo "Running... $i"
java -jar myjar.jar >> runresults.txt &
echo "Sleeping 2 seconds..."
sleep 2
done

Categories

Resources