Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Linux ›
How to manage jobs in Linux – fg bg kill Ctrl+Z

How to manage jobs in Linux – fg bg kill Ctrl+Z

July 2, 2012 Linux Jesin A 1 Comment

linux category thumbnail

How many times did you start something which is time consuming and waited for it to end until you could use the console again. We have all faced a situtation when we just stare at the screen for a process to get over after we have initiated it. It could be something as simple as copying a large file from a removable media or installing packages using yum/apt-get from online repositories over a slow internet connection (ouch!). Once started you will not get back the command shell until the process is done, so what do you do ? Hit Ctrl+C terminate it and hope to do it over the weekend ? Take a coffee break ? Count the number of keys on the keyboard ? Why not press Ctrl+Z and get the work done in background while you use the console to do something else. In this article I will cover how to suspend a process a.k.a. job, run it in the background, bring it to the foreground, terminate it if necessary.

Viewing the status of jobs

The “jobs” command will display background jobs and their current status.

root@debian:~# jobs
[1]+ Stopped apt-get upgrade
[2]- Running dd if=/dev/zero of=dummyfile bs=1K count=2048000 &

Executing a job in the background

If you are well aware that something will take a long time to finish why not start it in the background itself. Just adding an ampersand (&) after the command will execute it in background.

cp /mnt/largefile /home/jesin/files/largefile.bak &

Moving a foreground job to the background

You have started something thinking it will end soon but it is taking an eternity to complete, just a simple Ctrl+Z is enough to move it back

root@debian:~# dd if=/dev/zero of=dummyfile bs=1K count=2048000
^Z
[1]+ Stopped dd if=/dev/zero of=dummyfile bs=1K count=2048000

That ^Z denotes hitting Ctrl+Z. Now that the job is stopped use the bg command to run it in the background.

root@debian:~# bg %1

The number “1” is the ID of the job as viewed under the jobs command it must be preceded with a “%”

Moving a background job to the foreground

To bring a job running in the background to the foreground use the fg command

root@debian:~# fg %1

Terminating a job

Terminating or killing a job can be done using the “kill” command. But wait a minute isn’t this command used to terminate processes ? Yes it is so it all depends on the argument passed. The job number should be entered preceded by a percentage symbol (%)

root@debian:~# kill %2

Be very careful not to miss the percentage symbol, miss it and you will end up terminating PID 1 which is “init” or PID 2 which is “kthreadd” terminating any of these will make your life miserable.

Suspend a background job

Now this is a bit tricky as there is no direct command to make this happen, you need to use a couple of commands to stop a process running in the background. The process we follow is this – note the jobID, bring the job to the foreground, press CTRL+Z to suspend it.

root@debian:~# jobs
[1]+ Running dd if=/dev/zero of=dummyfile bs=1K count=2048000 &
root@debian:~# fg %1
^Z
[1]+ Stopped dd if=/dev/zero of=dummyfile bs=1K count=2048000

Related posts:

virtualization category thumbnailVMware Ctrl+Alt+[Function Key] linux category thumbnailReset Root Password in Linux change default runlevel in linuxHow to change the default runlevel in Linux linux category thumbnailHow to create a swap file in Linux linux category thumbnailForgot MySQL root user password (Linux)

Tags: linux, linux commands

Comments

  1. Arthur says

    August 6, 2015 at 9:26 pm

    Don’t forget disown - h %1 with bash. This let’s you close the shell and have the process still run.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.