Command line tips - controlling your processes

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 - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Command line tips - controlling your processes

Home » Articles » Command line tips - controlling your processes