8

I read about signals and I know how to send a signal to a process from the command line. Just do

ps -C executable

to know the pid of the process, and then

kill -s signal pid(number)

However, I'd like to find a more interactive and general way to do this if possible.

I'd like to send a signal to a specific process from the command line. I'd like to not need to print the PID of the process I want to signal, but rather use a way that the code understands which process should be signaled.

Other than that I'd like to understand exacly what the kill command does.

  • Does it kill the process that signals?
  • Does it kill the process where it's called?
  • If it kills a process it means that it's similar to do exit(0), or does the process resume after the signal is sent back?
2
  • So what would be you desired form of sending signal? Can you specify it by an example?
    – Eugene Sh.
    Oct 20, 2015 at 15:07
  • @EugeneSh. I'd say a command that would run a function in the code would be perfect. I'd be abble to create a function to send the signal without a problem. I'd still want to know what kill does though Oct 20, 2015 at 15:12

3 Answers 3

23

As far as I understood your question you want to signal a process by its name, not by its PID. This can easily be achieved by combining the two commands:

kill -s signal $(ps -C executable)


Does it kill the process that signals?

kill can kill. It doesn't necessarily.

From man kill:

The command kill sends the specified signal to the specified processes or process groups.

That means, the kill command is used to **send any signal in general.

If it kills a process it means that it's similar to do exit(0), or does the process resume after the signal is sent back?

From here:

The SIGKILL signal is used to cause immediate program termination. It cannot be handled or ignored, and is therefore always fatal. It is also not possible to block this signal.

If a process receives the SIGKILL signal, it terminates immediately (no destructors called, no cleanup done). The only processes that do not terminate are uninterruptible processes.


A full list of signals available on Linux is found here.

3
  • so I can use kill without a problem, and it will not pause or kill any process, as long as I don't use the SIGKILL signal? Oct 20, 2015 at 15:13
  • However, I have 3 processes, so I think your answer proposal would send the signal to all 3 of them, not 1 specific Oct 20, 2015 at 15:15
  • @AndréAlmeida kill only sends signals. There are other signals such as SIGSTOP or SIGTERM, which stop/terminate programs. You need to look each signal's function up to be sure what it does.
    – cadaniluk
    Oct 20, 2015 at 15:15
3

I prefer this:

pkill -signal processname

Here is a concrete example I use from time to time:

pkill -USR1 Squeak

See pkill - Wikipedia

0

I'd like to send a signal to a specific proccess from the command line. I'd like to not need to print the PID of the process I want to signal, but rather use a way that the code understands which process should be signaled.

The killall command meets those criteria, if you have it available to you. It allows you to specify the process(es) to signal based on their names. All of the following comments apply equally to the effects of delivering a signal via killall and delivering one via kill.

Other than that I'd like to understand exacly what the kill command does.

Does it kill the process that signals?

It delivers the specified signal. That's it. Some signals it can deliver will have the effect of killing the process.

Does it kill the process where it's called?

It delivers a signal only to the process you specify.

If it kills a process it means that it's similar to do exit(0), or does the process resume after the signal is sent back?

Processes can handle some signals without terminating. Exactly what happens when a process receives one of those signals depends on the process. If a process does not provide a handler for a signal whose default action is to terminate the process, then the process will die, never to resume. The effect is somewhat like that of calling the exit() function or returning from main(), but the termination is abrupt, without calling exit handlers or such.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.