Linux stop process in background

Linux: kill background task

This is in the overlap region between SO and SU, but I think it fits better here on SO. My criteria for thinking this way is that if @flybywire is doing this in a script, it’s programming. If he just wanted to do it from the command line I’d say it belongs on SU.

8 Answers 8

You can kill by job number. When you put a task in the background you’ll see something like:

That [1] is the job number and can be referenced like:

$ kill %1 $ kill %% # Most recent background job 

To see a list of job numbers use the jobs command. More from man bash :

There are a number of ways to refer to a job in the shell. The character % introduces a job name. Job number n may be referred to as %n . A job may also be referred to using a prefix of the name used to start it, or using a substring that appears in its command line. For example, %ce refers to a stopped ce job. If a prefix matches more than one job, bash reports an error. Using %?ce , on the other hand, refers to any job containing the string ce in its command line. If the substring matches more than one job, bash reports an error. The symbols %% and %+ refer to the shell’s notion of the current job, which is the last job stopped while it was in the foreground or started in the background. The previous job may be referenced using %- . In output pertaining to jobs (e.g., the output of the jobs command), the current job is always flagged with a + , and the previous job with a — . A single % (with no accompanying job specification) also refers to the current job.

Источник

How to Kill a Background Process in Linux

Linux is a multi-user and multi-task operating system. It supports more than one user and can run multiple processes simultaneously. Technically, that is not the case; the Linux kernel uses scheduling and other process management methods to assign a specific time to each process, making them appear to run simultaneously.

Читайте также:  Open files linux oracle

However, Linux allows us to perform tasks to the running processes, including background and foreground jobs.

This tutorial will discuss how to work with background processes and terminate them using various commands.

How to Run a Command in the Background

In most cases, when running a command from the Linux terminal, we wait for it to complete and exit. This functionality can be helpful for short commands or commands that require user interaction. However, in the case of processes that take a long time to complete, it can prevent you from running other commands.

Although pseudo-terminals can escape this, this becomes a problem in an sole terminal-based environment.

Use & Symbol

In Linux, one method to run a command in the background is to use the & symbol as:

The above syntax tells the shell to put whatever commands precede the ampersand in the background.

Once you put a process in the background, the shell will give you the job ID enclosed by a pair of square brackets and the PID (process ID).

Use CTRL + Z

Another method to put a process in the background is to use the CTRL + Z shortcut. Suppose we forgot to add the ampersand when running a program.

To put the said process in the background, we can press the CTRL + Z key and suspend the job. It is good to note that this does not terminate the process; it only freezes it.

To resume the process in the background, use the bg command:

As seen in the screenshot above, we run the Firefox process in the foreground, which “eats” our prompt until we terminate the process.

We freeze the process using the CTRL + Z shortcut and put it in the background using the bg command.

How to Show Running (and Stopped) Background Processes

To show the background processes, we use the jobs -l command:

The command will show both the running and stopped processes.

How to Bring a Background Process to the Foreground

To bring a background process in the foreground, you use the fg command followed by %[job id]

How to Kill a Background Process

Killing a background process is fairly straightforward; use the command pkill and the process ID, or process name as:

Читайте также:  Opt file in linux

Using the pkill command will force terminate (-9) the processes with the process name of ping.

Conclusion

This guide walked through the basics of job control using foreground and background processes.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

How to stop a bash while loop running in the background?

Notice the & . I thought when I killed the terminal, this while loop would stop. But it is still going. It has been hours since I killed the terminal session. How do I stop this while loop?

3 Answers 3

while true; do yad; sleep 60; done & 

and closed the terminal to test it, now I got the same problem.

If you already closed the terminal you’ve started the loop in

Let’s get an overview of running processes with ps fjx , the last lines on my machine read

 2226 11606 11606 19337 ? -1 S 1000 0:00 \_ /bin/bash 11606 22485 11606 19337 ? -1 S 1000 0:00 | \_ sleep 10 2226 9411 9411 8383 ? -1 S 1000 0:00 \_ /bin/bash 9411 17674 9411 8383 ? -1 Sl 1000 0:00 \_ yad 1 2215 2215 2215 ? -1 Ss 1000 0:00 /lib/systemd/systemd --user 2215 2216 2215 2215 ? -1 S 1000 0:00 \_ (sd-pam) 

You can see yad as a subprocess of /bin/bash , if I close yad it changes to sleep 60 in the output of ps . If the tree view is too confusing you can also search the output as follows 1 :

ps fjx | grep "[y]ad" # or respectively ps fjx | grep "[s]leep 60" 

The output lines begin with two numbers, the first being the PPID, the process ID of the parent process and the second being the PID, the ID of the process itself. It’s because of that 9411 appears in both rows here:

2226 9411 9411 8383 ? -1 S 1000 0:00 \_ /bin/bash 9411 17674 9411 8383 ? -1 Sl 1000 0:00 \_ yad

That’s the bash subshell we want to kill and we just found out its PID, so now everything that remains is a simple

kill 9411 # replace 9411 with the PID you found out! 

and the annoying subshell is gone for good.

Читайте также:  Регулировка скорости в линукс

1 : The notation as grep «[y]ad» instead of simply grep yad prevents the grep process itself from showing up in the results list.

If you have the terminal still open

bash provides the variable $! , which “expands to the process ID of the job most recently placed into the background”, so the following just kills the latest process in the background:

If it’s not the latest process, just can get a list of running jobs with the jobs builtin, example output:

[1]- Running while true; do yad; sleep 60; done & [2]+ Stopped sleep 10 

There are two jobs in the job list, to kill one of them you can access it with the job number or the shortcuts % , %+ (“current job”) and %- (“previous job”), e.g. to kill the loop running in the background you could do

and to kill the suspended sleep 10 job:

kill %2 # or kill %+ # or kill % 

Note that killing a process may not kill its subprocesses. On top of using kill %1 , one can use pkill -P $! for also killing all subprocesses.

I need to note that when you close the terminal where you entered that command, the sub-command should’ve died along with it. Trying to reproduce this showed this behavior.

Now, assuming that you indeed are in a situation where this didn’t happen, one way to go about killing the program you left running in the background, which was caused by the & at the end of the command, is as follows:

  1. Open a new shell and run echo $$ to note your current PID (process ID), so that you don’t kill your own session.
  2. Identify the PID of the program you think is still running; you can do this using the ps -ef | grep $SHELL command to find which programs are running a shell.
  3. Note the 2nd column from the left and note the numeric value that differs from your echo $$ result above; this is the PID you want to kill.
  4. Run the kill -SIGTERM command, where is the numeric value you identified in step #3
  5. Confirm the program is no longer running by repeating step #2

You could also restart your session or your computer, but the above will allow you to avoid that.

Источник

Оцените статью
Adblock
detector