Skip to main content

Processes

  • At all times with Linux a certain amount of processes will be running.
  • A process is any sort of command that's currently running.
  • For example, if you have a shell open, you're running bash as a process.

A process is a program that is running on your computer. It's a program that's been loaded into memory and is running.

  • Go ahead and run ps and see what it outputs.
ps
  • ps stands for processes snaphshot.
Processes ID
  • Every process you run is assigned a process ID which everyone refers to as a pid.
  • Every process is also owned by a user.
  • Some processes will always be owned by root, others by whatever user you are, and others still. If you look at your ps output, you'll those random numbers next to what you're running. This is the pid.
hassanali:~$ ps
PID TTY TIME CMD
57164 pts/1 00:00:00 bash
61893 pts/1 00:00:00 ps

Example

I am running the dummy process let show to you the blow command can sleep the process for 1000 seconds.

sleep 100

if we adding the & at the end of the command, it will run in the background.

sleep 100 &

So now we have two processes running.

hassanali:~$ ps
PID TTY TIME CMD
57164 pts/1 00:00:00 bash
61893 pts/1 00:00:00 ps
61900 pts/1 00:00:00 sleep

Kill a Process

  • If you want to kill a process, you can use the kill command.
  • You can kill a process by its pid.
kill PID
## Another command
kill -s SIGKILL <the pid from above>
kill 61900
## same working as above
kill -SIGKILL 61900
  • If you want to kill a process by its name, you can use the pkill command.
pkill sleep
  • If you want to kill all the processes by its name, you can use the killall command.
killall sleep
hassanali:~$ ps
PID TTY TIME CMD
57164 pts/1 00:00:00 bash
66596 pts/1 00:00:00 ps
hassanali:~$ sleep 100 &
[1] 66670
hassanali:~$ ps
PID TTY TIME CMD
57164 pts/1 00:00:00 bash
66670 pts/1 00:00:00 sleep
66685 pts/1 00:00:00 ps
hassanali:~$ kill 66670
hassanali:~$ ps
PID TTY TIME CMD
57164 pts/1 00:00:00 bash
66805 pts/1 00:00:00 ps
[1]+ Terminated sleep 100

So what else is your computer running right now?

Try

ps aux
  • This will show you everything running from everyone, including all system processes.
  • It should be substantially longer. You'll see a few processes running by you, many from root, and a few from others like systemd+, daemon, and others.
  • This list can be overwhelming so I'll frequently feed this into grep to find things I'm looking for.
    Try this:
ps aux | grep ps

You'll notice the list is pared down to just things have ps in them (including the grep command itself)

Foreground and Background

A process can either run in the foreground or the background.

  • If something is running in the foreground, you'll see all the output and you will wait until it's finished.
  • If it's running in the background, you can still see the output (unless you redirect it) but you can start doing other things.
  • When you put the & at the end it means "I want this to run in the background" (and hence why we've been using it before.)
sleep 100
# hit CTRL + Z
jobs # notice process is stopped
bg 1 # it's the first and only item in the list, the number refers to that
jobs # notice process is running
fg 1 # reattach to the process

The one thing to be careful with that is it doesn't necessarily send the output to the background.So if you're gonna do that, you're gonna have to redirect the output, right? What you would have to do here is like one or just send that all to output.txt and then do that.Otherwise, you're gonna still get the output to your screen despite the fact that your screen is still gonna be interactive.

sleep 1000 > output.txt &