Notas

Notas, scripts, algoritmos de uso diario y documentación de algunos proyectos.

View on GitHub
16 August 2021

Java Process

by Juan Manuel González Garzón

Java Process

I. Main.java

public class Main {
	public static void main(String[] args) throws InterruptedException {
		System.out.println("===========================================");
		for (int i=1; true; i++) {
			System.out.printf("Print %4d" + System.lineSeparator(), i);
			Thread.sleep(5*1000);
		}
	}
}

II. Compile.sh

#!/bin/bash

javac Main.java
echo 'Main-Class: Main' > MANIFEST.MF
jar cfm while-true.jar MANIFEST.MF Main.class
rm *.class && rm *.MF
chmod 740 while-true.jar

III. start.sh

#!/bin/bash

nohup java -jar while-true.jar >> "$(date +'%F').log" 2>&1 & disown
echo $! > process_pid.txt
echo "Runing process with pid: $(cat process_pid.txt)"

IV. stop.sh

#!/bin/bash

if [ -f process_pid.txt ]; then
	PID=$(cat process_pid.txt)
	kill -9 $PID
	rm process_pid.txt
	echo "Process stoped, pid: $PID"
else
	echo 'Nothing to stop'
fi

V. restart.sh

#!/bin/bash

echo 'Restarting process'
./stop.sh
./start.sh

Creditos y referencias

Java iconos creados por DinosoftLabs - Flaticon

tags: