1

I want to keep my java application running. however,

pkill java

is execute on teminal.

For Example, There is multiple java applications are running. and the above command kill all the java applications. But, I want an Application keep running. Please, suggest something which help me.

  • Does it kill all java applications or all "java.exe" processes? If it is just the "java.exe" processes then you could build an .exe for example, so you wouldn't have to use any "tricks" – Marcel Feb 19 '16 at 12:49
  • @ScriptKiddy I am using Linux environment. – Ajay Vaghela Feb 19 '16 at 12:50
  • Doesn't linux have any executeable files on its own? Because if u dont want to use such a file i guess u will have to change/influence the system behavior. – Marcel Feb 19 '16 at 12:51
  • @ScriptKiddy I am a developer and work for an organization. and it is a product of them. so if organization release the product then i am not able to change system behavior of all the clients – Ajay Vaghela Feb 19 '16 at 12:52
  • Oh , i didn't even think of that, sry. – Marcel Feb 19 '16 at 12:53
  • May i ask for what reason you shouldnt be able to kill the program like that? – Marcel Feb 19 '16 at 12:54
  • no, its fine i was explaining the case and it just post the comment by mistake. – Ajay Vaghela Feb 19 '16 at 12:54
  • 2
    Maybe use a shell script with trap? Could solve the problem as long as they don't send SIGKILL and your program does not use SIGTERM in any fancy way. – dryman Feb 19 '16 at 12:56
  • I have developed a product called DesktopEmergencyNotification which trigger Emergency Alerts on our user's computer. which may be used by a development organization. so if any of the user execute pkill java or kill the java process it may affect our poduct. – Ajay Vaghela Feb 19 '16 at 12:57
  • 1
    Depending on your init system, it is possible to write an init script which will restart the application whenever it is killed. This is probably the most robust solution. – fge Feb 19 '16 at 12:58
  • @dryman I didn't get you what you want to say. So, please can you elaborate what you want to say. – Ajay Vaghela Feb 19 '16 at 12:58
  • Note: maybe this question would be a better fit for superuser – fge Feb 19 '16 at 12:58
  • Sorry @fge i am not that much familiar with Linux. Can you give any references? – Ajay Vaghela Feb 19 '16 at 12:59
  • " Depending on your init system, it is possible to write an init script which will restart the application whenever it is killed. This is probably the most robust solution. – fge 1 min ago " if you do it like that the program will still be closed and all temporary data will be lost, am i right? – Marcel Feb 19 '16 at 13:00
  • Not really; it depends on the Linux distribution you use – fge Feb 19 '16 at 13:00
  • well if you say that it depends on the system he can't just do this cause as he said he is working for an company and their customers use whatever system they want – Marcel Feb 19 '16 at 13:01
  • I am using Ubuntu(14.04) is it possible to do that in it??@fge – Ajay Vaghela Feb 19 '16 at 13:01
  • There is a shell tool called trap which can catch signals. In this case you maybe can catch the normal SIGTERM signal and ignore it. But trap uses the whole environment therefore you should use a shell script to open up a new subenvironment. See http://steve-parker.org/sh/trap.shtml – dryman Feb 19 '16 at 13:01
  • I think the best way for this would be "Reprogram the user". Why would the user do `pkill java` if some things need to stay alive? – Ferrybig Feb 19 '16 at 13:02
  • yes @ScriptKiddy you are right. So, is there any other way? like executes some commands will help?? – Ajay Vaghela Feb 19 '16 at 13:03
  • @Ferrybig maybe the user needs to do that for some reason, but he wouldnt be able to cause the other program needs to be opened all the time – Marcel Feb 19 '16 at 13:03
  • @Ferrybig This application will be use by endusers who does not know in which environment application running on. so, they might kill the java processes. – Ajay Vaghela Feb 19 '16 at 13:04
  • @Ajay Vaghela look at what dryman commented, maybe that helps, but that is a linux only solution – Marcel Feb 19 '16 at 13:05
  • Is there any ways by which i can handle it from java code within the application? – Ajay Vaghela Feb 19 '16 at 13:06
  • probably the system would think your program is a virus if u go that far ^^ – Marcel Feb 19 '16 at 13:07
  • maybe u can execute it like a "windows service" does linux also have this kinds of services? i am not sure , but maybe it wont be closed like that – Marcel Feb 19 '16 at 13:08
  • @ScriptKiddy as dryman says. if ii use their suggestion then also i have to execute that script from my java code only. – Ajay Vaghela Feb 19 '16 at 13:09
  • I have a question, if you use "pkill java" does it kill all the java processes or just the processes that got "java" in(as) their name – Marcel Feb 19 '16 at 13:19

1 Answers1

0

I think you can find sample code that will answer your question in Signal handling using "TERM"

TL&DR: You have to create your own 'signal' handler and register it with JVM runtime and your handler will ignore normal pkill (aka kill -3), but kill -9 still will be able to shutdown your app.

BTW: you can set name for your java application, so it will 'hide' from pkill java

Use following to to start your application

exec -a your_name java -cp ...

Community
  • 1
  • 1
ya_pulser
  • 2,620
  • 2
  • 16
  • 21