Command line tips - controlling your processes

  • March 12, 2007
  • Avatar for peter
    Peter
    Upfold

When you're running a command that's going to take a long time in your bash shell, but then you suddenly decide you don't want to run it any more, CLI newbies can often be stuck as to how they should terminate a running command (aside from closing the terminal window). There are also other occasions when you want to control the process that's running inside your terminal.

This post is going to give you a quick run-down of some of the most common key combinations that perform useful actions like terminating processes and setting them to run in the foreground and background.

  • You want to stop a process - pressing Ctrl-C inside your terminal window sends a signal to the program you're running, telling it to stop what it's doing and shut down. In a lot of cases, this works to stop something from happening (perhaps you realise you've just copied a file into the wrong place).
  • Suspending a process - press Ctrl-Z to suspend the currently running process. It won't execute until you unsuspend it, but you don't need to start it again from scratch either. Look out for the number that will appear inside square brackets, you'll use this to refer to this 'job' later.
  • Running things in the foreground - if you've suspended a process and you want to start it again, run fg n where n is that number in square brackets you saw earlier.
  • Running things in the background - likewise, to continue running a suspended process in the background, type in bg n where n is that number in square brackets you saw earlier.

If you ever get stuck and forget which number refers to which job, run this command:

$ jobs

This is just a really really quick look at some of the ways you can manage all your different CLI tasks, and I'm sure there are bash experts out there who could offer even more insight into having complete control over what you're doing. So if you have any good tips, let us know in the comments.

Avatar for peter Peter Upfold

Home » Articles »