Process manipulation

This section is going to be devoted to the manipulation of processes. I’m not going to go into any detail about how processes are created or how they run, only how to find, manage, kill, etc., them.

The commands I’ll show examples for are: jobs, pidof, pgrep, ps, kill, pkill, and killall. I’m going to preform all the examples on the sleep command, which is something you can do by running sleep in the background:

user@computer ~$ sleep 1000 &
[1] 12659

JOBS

To see what processes you have started, you can use:

user@computer ~$ jobs
[1]+ Running sleep 1000 &

PIDOF

First up is the pidof command. As its name indicates, it finds the process id of whatever process name you pass it as an argument.

user@computer ~$ pidof sleep
12659

PGREP

Personally, I like pgrep more. It has more options and flexibility. To simply find the pid (process id), you issue:

user@computer ~$ pgrep sleep
12659

If you want a little more information, you can pass the -l switch

user@computer ~$ pgrep -l sleep
12659 sleep

For even more information (i.e. the entire original command issued), use -a:

user@computer ~$ pgrep -a sleep
12659 sleep 1000

Another very helpful feature is to use the -d option to delimit the ids, say by a comma (useful for piping into another command like kill)

user@computer ~$ sleep 1234 &
[2] 12699
user@computer ~$ pgrep -d, sleep
12659,12699

Finally, the -u switch shows all the process running by a particular user.

user@computer ~$ pgrep -u user
1958
1963
...

PS

The ps command is next. It has a lot of different switches, though I’ll only cover a few here.

-C lists the process info

user@computer ~$ sleep 1000 &
[1] 12787
user@computer ~$ ps -C sleep
 PID TTY TIME CMD
12787 pts/2 00:00:00 sleep

-p does the same thing, only using the pid rather than the name

user@computer ~$ ps -p 12787
 PID TTY TIME CMD
12787 pts/2 00:00:00 sleep

-fp adds full format listing

user@computer ~$ ps -fp $(pgrep -d, sleep)
UID PID PPID C STIME TTY STAT TIME CMD
selene 12925 12636 0 20:44 pts/2 S 0:00 sleep 1000
selene 12926 12636 0 20:44 pts/2 S 0:00 sleep 1234

-u works just like pgrep -u

user@computer ~$ ps -u user
1958
1963
...

And the last switch, -fx (or -fax to show all processes) prints a nice ASCII syntax tree of the processes

 PID   TTY   STAT  TIME   COMMAND
12630  ?     Sl    0:02   gnome-terminal
12635  ?     S     0:00   \_ gnome-pty-helper
12636  pts/2 Ss    0:00   \_ bash
12787  pts/2 S     0:00   \_ sleep 1000
12798  pts/2 R+    0:00   \_ ps -fx

KILL

Kill is an easy one to use, though it has quite a few different signals it can send to a process. The basic usage to kill a process with id 1234, is:

user@computer ~$ kill 1234

Note that this command returns no output if successful. Now for the signals. There are a lot of them, which you can list with:

user@computer ~$ kill -l
 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX

If this is overwhelming, don’t worry. There are really only five you need to remember.

1       this forces the process to reread its configuration file
9       this tells the kernel to kill the process, something that is unblockable by the process itself
15      this is the default, the signal sent if no other signal is specified. it asks the process to kill itself
19      this suspends a running the process
18      this continues a suspended process

The -1 switch is most useful to force init (always process 1, though with systemd taking over this, like a lot of things are soon to be obsolete) to reread its configuration file

user@computer ~$ -1 1

The next is the kernel kill, which is unblockable.

user@computer ~$ -9 1234

Using the sleep command as an example, we will suspend, recontinue the process, and finally ask it to kill itself.

user@computer ~$ sleep 1000 &
[1] 12864
user@computer ~$ kill -19 12864
[1]+ Stopped sleep 1000
user@computer ~$ kill -18 12864
user@computer ~$ jobs
[1]+ Running sleep 1000 &
user@computer ~$ kill -15 12864
user@computer ~$ jobs
[1]+ Terminated sleep 1000

PKILL

This kills a process by its command name

user@computer ~$ pkill sleep

You can also use it to issue the -1 signal

user@computer ~$ pkill -HUP init

KILLALL

This does the same as pkill, but kills all process of a certain name and with the -15 signal

user@computer ~$ killall sleep
[1]- Terminated sleep 1000
[2]+ Terminated sleep 1234

And that’s pretty much it for the intro tutorial to process management. Hopefully you’ve learned some new tricks to dealing with the processes on your computer.

Tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam