UNIX / Linux: 3 Ways to Send Signal to Processes

by Balakrishnan Mariyappan on February 8, 2011

Question: How do I send signal to another process? Can you explain me all available options to send signals to a process in UNIX / Linux environment?

Answer: You can send various signals to processes using one of the methods explains in this article.

1. Send Signal to a Process Using Kill

Use kill command to send a signal to a process. For example, if you want to send USR1 signal to the process “a.out”, do the following.

$ ps -C a.out
  PID TTY          TIME CMD
 3699 pts/1    00:00:00 a.out

$ kill -s USR1 3699

Note: Refer to 4 Ways to Kill a Process – kill, killall, pkill, xkill.

2. Send Signal to a Process from Another Process

You can use the UNIX system call kill (from a C program) to send signal from one process to another. The following C code snippet shows how to use the kill command.

Kill system call takes two arguments: 1) the PID (process id) of the process that needs to be signalled 2) the signal that needs to be send to the process. Kill command returns 0 when it is successful.

int send_signal (int pid)
{
        int ret;
        ret = kill(pid,SIGHUP);
        printf("ret : %d",ret);

}

3. Send Signal to a Process from Keyboard

When a process is running on the terminal, you can send signal to that process from the keyboard by using some specific combination of keys. The following are couple of examples.

  • SIGINT (Ctrl + C) – You know this already. Pressing Ctrl + C kills the running foreground process. This sends the SIGINT to the process to kill it.
  • You can send SIGQUIT signal to a process by pressing Ctrl + \ or Ctrl + Y

You can view the key mappings that sends specific signal to a process using the “stty -a” command as shown below.

$ stty -a | grep intr
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?;

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 4 comments… read them below or add one }

1 Kuldeep February 8, 2011 at 4:14 am

Thanx alot sir, for very helpful information…..

2 SaveTheRbtz February 8, 2011 at 4:52 am

Info about signals 0 and -1 may be usable here.

3 Ganesh February 9, 2011 at 6:15 am

hello sir how to install vlc in linux 5

4 Felix Frank February 11, 2011 at 6:39 am

Hi.

intr and quit are signals, I think, the rest are not. Specifically, there is no shortcut to send a kill signal.

Also I’d like to add the other useful shortcut: Ctrl+Z for SIGSTOP. Followed by “bg”, it can be used to transfer a foreground process to the background.

Regards,
Felix

Leave a Comment

Previous post:

Next post: