0

I have a process generator that make some processes working in linux (this code is by java) but during these processes working I want to make some interrupt to change the process config.

If I use an spooling method , It have too many overflow So I want to use another method to make some interrupts into other processes.

eis
  • 51,991
  • 13
  • 150
  • 199
Mohammad
  • 1,474
  • 2
  • 11
  • 20

2 Answers2

3

Since the answer linked by @Vlad is for Windows, and this question is for linux, here's an answer to that. Linux uses signals to interrupt processes, and you can use kill to send a signal:

// shut down process with id 123 (can be ignored by the process)
Runtime.getRuntime().exec("kill 123");
// shut down process with id 123 (can not be ignored by the process)
Runtime.getRuntime().exec("kill -9 123");

with kill, you can also send other signals as told in the man page (and it doesn't have to be a killing signal). By default, kill will send a SIGTERM signal, which tells the process to terminate, but it can be ignored. If you want the process to terminate without the possibility for ignore, SIGKILL can be used. In the code above, first call uses SIGTERM, the latter one SIGKILL. You can also be explicit about it:

// shut down process with id 123 (can be ignored by the process)
Runtime.getRuntime().exec("kill -SIGTERM 123");
// shut down process with id 123 (can not be ignored by the process)
Runtime.getRuntime().exec("kill -SIGKILL 123");

If you want to operate with name of the target program instead of process id, there's also killall which will accept the name as a parameter. As the name implies, that would kill all processes that match. Example:

// shuts down all firefox processes (can not be ignored)
Runtime.getRuntime().exec("killall -SIGKILL firefox");
eis
  • 51,991
  • 13
  • 150
  • 199
1

To kill the process get the pid of that process using the below command ps -ef | grep 'process name' use the pid to kill the process where pid is 16804
Ex :

[root@localhost content]# ps -ef | grep tomcat
root     16804     1  0 Apr09 ?        00:00:42 /usr/bin/java -Djava.util.logging.config.file=/usr/local2/tomcat66/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms1024m -Xmx1024m -/usr/local2/tomcat66/bin/bootstrap.jar -Dcatalina.base=/usr/local2/tomcat66 -Dcatalina.home=/usr/local2/tomcat66 -Djava.io.tmpdir=/usr/local2/tomcat66/temp org.apache.catalina.startup.Bootstrap start

Then in java use the command

1. Runtime.getRuntime().exec("kill -15 16804"); // where -15 is SIGTERM 
or
2. Runtime.getRuntime().exec("kill -9 16804"); // where -9 is SIGKILL

Check this for various Killing processes and this for killing signals

Kiran Jujare
  • 178
  • 10