Need help scripting with jar files

user2656801 just a moment. 1 answers, 0 views
bash java

I need to launch 17 .jar files, one at a time, with a 7 second delay in between each. 3 hours later, I need to kill all java processes, but only those running on surge user. 3 hours later I need to repeat this process.

So I have something like this in a Windows Batch file.

start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
timeout /t 10800
taskkill /F /IM java.exe /T
timeout /t 10800

start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 
start java -Xmx4G -jar C:\Users\surge\Downloads\cooljar.jar
timeout /t 7 

Now I need it in Linux shell script format.

So I've been using

nohup java -jar cooljar.jar && nohup java -jar cooljar.jar && nohup java -jar cooljar.jar. 

But that does not allow a delay between each launch.

edit: sorry I didn't mention this, the jars have different arguments.

Edit @Byte Commander: I am trying your example like this. It's not working.

#!/bin/bash

launch_jars() {

    java -jar cooljar.jar -n 1
    sleep 7
    java -jar cooljar.jar -n 2
    sleep 7
    java -jar cooljar.jar -n 3
    sleep 7
    java -jar cooljar.jar -n 4
    sleep 7
    java -jar cooljar.jar -n 5
    sleep 7

}

while 1 ; do
    launch_jars
    sleep 3h
    killall -u surge java
    sleep 3h
done

and I am getting errors:

$ bash /home/surge/Downloads/run.sh
/home/surge/Downloads/run.sh: line 2: $'\r': command not found
/home/surge/Downloads/run.sh: line 3: syntax error near unexpected token `$'{\r'
'home/surge/Downloads/run.sh: line 3: `launch_jars() {
$

1 Answers


Byte Commander 07/15/2016.

Assuming you launch your Java JAR using java -jar cooljar.jar, you could use this script:

#!/bin/bash

launch_jars() {
    for i in {1..17} ; do
        java -jar cooljar.jar &
        sleep 7
    done
}

while 1 ; do
    launch_jars
    sleep 3h
    killall -u surge java
    sleep 3h
done

This defines the function launch_jars which launches the same JAR file 17 times in a row with a delay of 7 seconds in between.

It will run an infinite loop of calling that function, waiting 3 hours, killing all java processes of user surge and waiting 3 more hours.

Copy the script and save it using a text editor like gedit as e.g. jar-launcher.sh. Then make it executable by running the terminal command

chmod +x jar-launcher.sh

You can then run the script. To abort it, hit Ctrl+C.

Language

Popular Tags