HowTo: Use ps, kill, nice, and killall To Manage processes in FreeBSD and OS X Unix Operating System
I‘m a new Unix system user. How can I manage process on a FreeBSD operating systems?
A process is nothing but an executing program on FreeBSD or Unix-like system. Each process on the system provides the resources needed to run a program such as vim or firefox. Each process has: [donotprint][/donotprint]
A unique process identifier number (PID)
A virtual address space
A security context
Open devices/handles, executable code
Environment variables, priority and more.
In this quick tutorial, I will write about process management on a FreeBSD operating systems.
View running processes in FreeBSD
The ps or process status command is a command that lists process activities. Many people use the top command to interactively list the processes, but for scripting purpose ps is user friendly. The basic syntax is as follows: $ ps Sample outputs:
List process using ps command
Example using flags: $ ps -auxw
a = Display information about other users’ processes as well as your own.
u = Display the processes belonging to the specified usernames.
x = When displaying processes matched by other options, skip any processes which do not have a controlling terminal. This is the default behavior.
w = Use 132 columns to display information, instead of the default which is your window size.
How to list process using top command
Another easiest way to find out what processes are running on FreeBSD is to type top command: $ top Sample outputs:
To quit from the top command type (or hit) q and to display help hit h.
Quickly finding the PID of a process on FreeBSD
Use the pgrep command to search the process table on the running system and prints the process IDs (PID) of all processes that match the criteria given on the command line. The syntax is:
pgrep -u user process
pgrep -u www-data nginx
pgrep -u user process
pgrep -u www-data nginx
A quick way of getting the PID of a process called dhclient is with the pgrep command, type: $ pgrep dhclient Sample outputs:
How to send processes signals in FreeBSD Unix system
You can send signals to all processes in FreeBSD. For example, SIGTERM can gracefully kill the process.
Send processes signals by PID
The kill command kills process by process identification (PID). Note that the user must have sufficient privileges to kill the process. The basic syntax is as follows: $ kill <PID> $ kill -s signalName <PID> For example: $ kill -s SIGTERM 454 $ kill -signalName <PID> $ kill -SIGHUP 6765 $ kill -signalNumber <PID> $ kill -9 868 To find your PID use pgrep command. It will look like this for example: $ pgrep firefox 23872 From there you can issue the kill command to kill firefox process: $ kill 23872 You can also send multiple kills. $ kill <PID> <PID> <PID> $ kill 3455 79754 3454 If you want to kill without cleaning up you use the -9 signal: $ kill -9 <PID> $ kill -9 5858
Type the following command: $ kill -l Sample output:
Some of the more commonly used signals and meaning
The basic syntax is as follows: $ killall process $ killall firefox $ killall -SIGNAL process $ killall -term firefox $ killall -15 command $ killall -15 firefox To kill user’s process: $ killall -u <user> process $ killall -u tom firefox Depending upon your shell you might need different flags or signals. Consult your man pages (man signal). The examples are so you get an idea and not absolute. For example, on my laptop using csh I cannot use the command killall process. I must use at least one option or argument to specify process. Note, there is also pkill command which kills the processes by name, but since it uses pattern matching and not the precise process name, it can be potentially dangerous compared to killall. Here is an example of dangerous pkill command which kills FreeBSD based server or desktop: $ sudo pkill -v firefox The -v option reverse the sense of the matching; display processes that do not match the given criteria. See pkill man page for more info.
How to adjust process priorities
The nice command runs process at a low priority. nice assumes the value of 10. The priority is a value range -20 to 20. The default priority is 0, priority 20 is the lowest possible. Negative numbers are expressed by -. You need to be root if you want to change the process prority to higher priorities. Different shells are different so it’s best to consult your nice manual page. I mainly use csh. nice command is more useful on servers or workstation where several processes are demanding more resources than the CPU can provide. Use following commands to set a larger chunk of the CPU time than a lower priority process on a FreeBSD.
nice command usage examples
Execute command date at priority 5 assuming the priority of the shell is 0. $ nice -n 5 date This is an example where you need to be superuser or root: # nice -16 nice --35 date Execute command date`at priority -19 assuming the priority of the shell is 0. I have never had to use this command because with today’s systems we have such high RAM+CPU and processing power these things aren’t a problem like when first introduced. It’s nice to know nice though just in case.
How to alters the scheduling priority of running processes
The nice command only works when beginning a new process i.e. start of the command. Use the renice command to alters the scheduling priority running processes. The syntax is: $ renice priority pid $ renice -n increment pid $ renice -u user pid To Change the priority of process ID’s 4242 and 344, and all processes owned by root, enter: $ sudo renice +1 -u root -p 344 4242
How to see realtime and idle priority of of running processes
A process with a realtime priority is not subject to priority degradation, and will only be preempted by another process of equal or higher realtime priority. A process with an idle priority will run only when no other process is runnable and then only if its idle priority is equal or greater than all other runnable idle priority processes. Priority is an integer between 0 and RTP_PRIO_MAX (usually 31). 0 is the highest priority. 31 is the lowest priority. To see which realtime priority the current process is at, just type the following command: $ rtprio Sample outputs:
rtprio: normal priority
To see which realtime priority of process 715: $ rtprio 715 To change the realtime priority of process 715 to 16, enter: # rtprio 16 -1423 To run backup.py script while not disturbing other server usage, enter: # idprio 31 /path/to/my/awesome/backup.py See rtprio command man page for more info.
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.