Processes and Jobs Guide

+6 Bucky Roberts · January 13, 2016
Process - running program

# list processes
ps



  • PID - process ID

  • TTY - terminal where process is running

  • STAT - state (sleeping or running)

  • TIME - amount of CPU time processor spent running instructions

  • COMMAND - name



# all your running processes
ps x

# all system processes, not just ones you own
ps ax

# include detailed info
ps u

# show full command names
ps w

Controlling Jobs



  • Usually when you run a command, you don’t get the prompt back until the command is finished.

  • You can however tell a process to run in the background (useful if it takes a long time)



# Just a dumb sample program we can play with
xlogo



  • Shell does not return because the program is still running

  • Ctrl+C to send interrupt signal (ask it nicely to stop)



# To run to process in the background so we can keep using our shell
xlogo &



  • Returns job number and process ID

  • Process that we launch in this way can be referred to as jobs (assigned a special job number)



# To list the jobs that we launched from the terminal
jobs

What if we want to stop it? Since it’s in the background we can’t control it with the keyboard

# Bring a job to the foreground (percent sign then job number)
fg %1
Ctrl+C


# Stop (aka pause/freeze) a process
xlogo
Ctrl+Z


We get shell back, but can’t close the window because it is paused

# To unpause
jobs
fg %1


Note: Since a job is just a type of process, you can also use the process ID

# To kill (terminate) a process, send it a kill signal
xlogo &
ps
kill pid


# just freeze, don’t kill completely
xlogo &
ps
kill -STOP pid
ps


# to resume a stopped process
kill -CONT pid
ps
kill pid


Notes:


  • Ctrl + C is the same as using kill

  • When you kill a process, you are actually sending it a signal

  • That way, the process can do things (like save files it needs to) before it closes

  • You can only kill processes you own

  • To kill processes you don’t own, you need to have superuser privileges


Post a Reply

Replies

Oldest  Newest  Rating
0 Arch38 ツ · June 17, 2016
Pretty good guide, thanks Bucky :)
0 jiggunjer jiggunjer · August 29, 2016
Good follow-up subjects: 
- standard out/err/in and control signals (SIGINT, SIGSTOP, SIGHUP, etc.)
- how to close the terminal without killing jobs (e.g. nohup, disown)
- Bash subshells (e.g. `(foo&)` ) 
- Terminal multiplexing (e.g. tmux or screen) -- basically the 'minimize' equivalent for terminals
  • 1

Linux

111,989 followers
About

Linux is a free and open source software operating system for computers.

Links
Moderators
Bucky Roberts Administrator