Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting and killing java app with shell script (Debian)

I'm new to UNIX. I want to start my java app with a script like so:

#!/bin/sh
java -jar /usr/ScriptCheck.jar &
echo $! > /var/run/ScriptCheck.pid

This is supposedly working. It does run the app and it does write the pid file. But when I try to stop the process with a different script which contains this:

#!/bin/sh
kill -9 /var/run/ScriptCheck.pid

the console gives me this error:

bash: kill: /var/run/ScriptCheck.pid: arguments must be process or job IDs

My best guess is that I'm not writing the right code in the stop script, maybe not giving the right command to open the .pid file. Any help will be very appreciated.

like image 797
rMaero Avatar asked Jan 16 '12 18:01

rMaero


People also ask

How do I stop and start Java in Linux?

Kill a process by the pkill command For example, we want to kill all the processes with matching name java, execute the command as follows: pkill java.


3 Answers

You're passing a file name as an argument to kill when it expects a (proces id) number, so just read the process id from that file and pass it to kill:

#!/bin/sh 
PID=$(cat /var/run/ScriptCheck.pid) 
kill -9 $PID
like image 61
milan Avatar answered Oct 07 '22 13:10

milan


A quick and dirty method would be :

kill -9 $(cat /var/run/ScriptCheck.pid)
like image 38
Alex Avatar answered Oct 07 '22 12:10

Alex


Your syntax is wrong, kill takes a process id, not a file. You also should not be using kill -9 unless you absolutely know what you are doing.

kill $(cat /var/run/ScriptCheck.pid)

or

xargs kill </var/run/ScriptCheck.pid
like image 36
tripleee Avatar answered Oct 07 '22 13:10

tripleee