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:
|Requirements||FreeBSD or Unix-like os|
|Est. reading time||10m|
- 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:
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:
last pid: 762; load averages: 0.35, 0.30, 0.20 up 0+00:17:04 11:09:01 15 processes: 1 running, 14 sleeping CPU: 0.0% user, 0.0% nice, 0.0% system, 0.4% interrupt, 99.6% idle Mem: 13M Active, 16M Inact, 65M Wired, 23M Buf, 3624M Free Swap: 4071M Total, 4071M Free PID USERNAME THR PRI NICE SIZE RES STATE TIME WCPU COMMAND 736 ec2-user 1 20 0 17064K 2660K wait 0:00 0.00% sh 734 ec2-user 1 20 0 25752K 3024K pause 0:00 0.00% screen 714 ec2-user 1 20 0 86472K 7228K select 0:00 0.00% sshd 735 ec2-user 1 20 0 25752K 4432K select 0:00 0.00% screen 712 root 1 20 0 86472K 7228K select 0:00 0.00% sshd 602 root 1 20 0 24104K 5292K select 0:00 0.00% sendmail 411 root 1 20 0 14492K 2136K select 0:00 0.00% syslogd 715 ec2-user 1 20 0 17064K 2668K wait 0:00 0.00% sh 762 ec2-user 1 20 0 21916K 2816K RUN 0:00 0.00% top 657 root 1 20 0 16592K 2256K nanslp 0:00 0.00% cron 627 root 1 20 0 61204K 6556K select 0:00 0.00% sshd 333 root 1 20 0 13164K 4508K select 0:00 0.00% devd 294 root 1 52 0 14624K 2236K select 0:00 0.00% dhclient 605 smmsp 1 52 0 24104K 4952K pause 0:00 0.00% sendmail 332 _dhcp 1 52 0 14624K 2340K select 0:00 0.00% dhclient
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 process pgrep -u user process pgrep firefox 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
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>
$ 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
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
Some of the more commonly used signals and meaning
----------+---------------------------------------------- SIGNAL | NAME (Meaning) ----------+---------------------------------------------- 1 | HUP (hang up) 2 | INT (interrupt) 3 | QUIT (quit) 6 | ABRT (abort) 9 | KILL (non-catchable, non-ignorable kill) 14 | ALRM (alarm clock) 15 | TERM (software termination signal) --------------------------------------------------------
To kill process by name, use killall command
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: 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.
This quick tutorial was contributed by Wendy Michele. You can too contribute to nixCraft.
🐧 4 comments so far... add one ↓
|Category||List of Unix and Linux commands|
|Disk space analyzers||df • duf • ncdu • pydf|
|File Management||cat • cp • mkdir • tree|
|Firewall||Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04|
|Modern utilities||bat • exa|
|Network Utilities||NetHogs • dig • host • ip • nmap|
|OpenVPN||CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04|
|Package Manager||apk • apt|
|Processes Management||bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop|
|Searching||ag • grep • whereis • which|
|User Information||groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w|
|WireGuard VPN||Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04|