Linux stop stopped jobs

How can I kill all stopped jobs?

When I try to exit from my Linux server I get the message: There are stopped jobs. : Is there a single command to kill these?

I had opposite problem now, it would not warn about stopped jobs when I quit the shell! Had to put shopt -s checkjobs in my .bashrc

8 Answers 8

To quickly kill all the stopped jobs under the bash, enter:

jobs -ps lists the process IDs ( -p ) of the stopped ( -s ) jobs.
kill -9 `jobs -ps` sends SIGKILL signals to all of them.

didn’t work when the job was «sudo su». i.e. sudo kill `jobs -p` didnt work but explicitly typing PID did.

Why is this upvoted so highly? It’s wrong. If the processes are stopped, then a kill such as this will do nothing, since the processes are stopped, they won’t process the SIGTERM (-15) that is sent to them by default.

That should do it, but I think he should send first a SIGTERM (-15) before sending a SIGKILL (-9). So perhaps proposing something like «kill $(jobs -p) ; sleep 3s ; kill -9 $(jobs -p)» would be better. Sending SIGTERM first the jobs may be able to do a clean exit (freeing allocated resources, etc).

Kevin Duke, your answer is the one that worked for me. I couldn’t vote because I don’t have 15 in reputation. kill -9 $(jobs -p)

The accepted answer would kill all jobs (which is sufficient in this case) and not merely the stopped ones. Should you want to kill only the Stopped ones, run:

kill $(jobs -l | grep Stopped | cut -d' ' -f3) 

grep/cut can be combined in one awk command: awk ‘/Stopped/‘)

The easiest way is actually to simply immediately retry the exit; bash will take that to mean «kill all stopped jobs and exit».

for x in `jobs -p` ; do kill -9 $x ; done 

Can you take a look at your formatting again — use four spaces at the start of the line to mark a block as code (rather than using backtick). Right now it isn’t clear whether you are using backticks in your code, or trying to display code using backticks.

The formatting is fine. we have to pass the jobs -p using backticks else it wont consider as a command, and it will throw an error.

Normally if you got that message, you need to logout twice. E.g. first Ctrl+D gives you the warning message to inform you about stopped jobs, pressing for the second time will log you out killing the jobs. This the same applies to logout and exit commands.

To kill them manually, try: kill $(jobs -p) .

If you don’t want to kill jobs from your current shell, you can remove them from the table of active jobs without killing by using disown command. E.g.

$ sleep 1000 & [1] 19404 $ jobs [1]+ Running sleep 1000 & $ disown 

Stopped jobs also can be determined by the state of the process ( T character) which means the process was stopped by signal such as SIGSTOP , SIGTSTP or other (like SIGTTIN , or SIGTTOU ).

In case when jobs a shell builtin command is not available, stopped processes can be listed by the following command:

Читайте также:  Centos arm linux gcc

To kill them all, you can basically type:

kill -9 $(ps wuax | awk 'NR>1 && $8 ~ "T" ') 
$ sleep 1000 & [1] 2014 $ sleep 1000 & [2] 2015 $ sleep 1000 & [3] 2016 $ sleep 1000 & [4] 2017 $ killall -STOP sleep [1] Stopped sleep 1000 [2] Stopped sleep 1000 [3] Stopped sleep 1000 [4] Stopped sleep 1000 $ ps wuax | awk '$8 ~ "T"' USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND vagrant 2014 0.0 0.0 7228 832 pts/0 T 20:38 0:00 sleep 1000 vagrant 2015 0.0 0.0 7228 708 pts/0 T 20:38 0:00 sleep 1000 vagrant 2016 0.0 0.0 7228 760 pts/0 T 20:38 0:00 sleep 1000 vagrant 2017 0.0 0.0 7228 828 pts/0 T 20:38 0:00 sleep 1000 $ kill -9 $(awk 'NR>1 && $8 ~ "T" ' <(ps wuax)) $ jobs [1] Killed sleep 1000 [2] Killed sleep 1000 [3] Killed sleep 1000 [4] Killed sleep 1000 

Just in case this helps someone else -- most people are here because they have some stopped processes that they started, backgrounded via the shell maybe. I needed to find processes, as root, stopped by other users, for which variants on the jobs command won't do.

A bit of digging around with man ps got me to this:

ps -a -o pid,user,cmd,state | grep 'T$' 

Explanation: the -a flag says show all processes, then -o controls output, what info will be shown about each process. I'm choosing pid , user , cmd (the command line), and state , which is the process state.

PROCESS STATE CODES Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process: D uninterruptible sleep (usually IO) R running or runnable (on run queue) S interruptible sleep (waiting for an event to complete) T stopped, either by a job control signal or because it is being traced W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z defunct ("zombie") process, terminated but not reaped by its parent 

so finally I pipe it to grep T$ which says, show me all the processes that have T in the last column.

And then I have a nice list of all the processes from different users that are in the stopped state.

$ ps -a -o pid,user,cmd,state | grep 'T$' 865 joson74+ python T 885 joson74+ sh -c less T 886 joson74+ less T 1014 minames+ python3.4 -i /home/minames T 5352 MooKo nano stdio.h T 7851 harry tmux attach T 12083 harry tmux attach T 13495 gorylla+ python3.4 -i /home/gorylla1 T 18009 conr1d vim T 19664 enythin+ python T 24906 wardlist python T 

Источник

How to Kill All Stopped Jobs in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

Killing a stopped job or process is not a difficult task. Usually, we just need to call the kill command with the process ID (PID). However, sometimes Linux may hide the processes depending on which terminal launched them and whether they have higher privileges.

Читайте также:  Linux what does bash do

In this tutorial, we’ll discuss how to target and kill these processes in different scenarios using several commands.

2. Killing All Stopped Jobs From the Same Terminal

It’s rather easy to kill stopped processes launched from the same terminal. All we need to do is find the process IDs of the jobs and then send the SIGKILL signal to them.

For example, we can use the sleep command to initiate a task that does nothing:

This program will occupy the terminal for 1 minute. After running the command above, we interrupt it using CTRL+Z to send the SIGTSTP signal:

$ sleep 1m ^Z [1]+ Stopped sleep 1m 

Now, we have a stopped job. Next, we can use the jobs command to list that job:

Here, we use –p for getting the process ID, while –s filters only for processes that are stopped.

Additionally, we can use the -l parameter instead of -p for a more detailed view of the stopped job:

$ jobs -ls [1]+ 4772 Stopped sleep 1m 

The jobs -ls command will show all the stopped jobs launched in this session. Once we find their process IDs, we can use the kill command to stop all of them by providing multiple process IDs to the kill command, or we can directly pipe the jobs command after the kill command like this:

Importantly, it’s best to use the SIGKILL, not the SIGTERM signal, as code can easily catch or block the latter.

3. Killing All Stopped Jobs From Another Terminal

Sometimes we don’t have direct access to the terminal running a particular job. In these cases, it’s convenient to have a mechanism to identify and stop such jobs.

Linux does not let us see a terminal-launched process from another terminal with the jobs command. Because of this, we’ll use the ps command instead.

Let’s create two instances of the previously described process first:

$ tty /dev/pts/0 $ sleep 1m ^Z [1]+ Stopped sleep 1m $ sleep 2m ^Z [2]+ Stopped sleep 1m $ bg [2]+ sleep 2m &

Note the use of tty to show the terminal we’re using. Further, we use bg to ensure that only the first job is stopped while the last one is running in the background.

We can use the ps command in another terminal to see the newly created jobs. In fact, the flags aux ensure we see all processes (a) under the current user (u) with their states (x):

$ tty /dev/pts/1 $ ps aux | grep [s]leep username 5767 0.0 0.0 2364 572 pts/0 T 15:23 0:00 sleep 1m username 5768 0.0 0.0 2364 580 pts/0 S 15:24 0:00 sleep 2m 

Of course, we can filter by process name with grep. Furthermore, to identify the correct process, we need to pay attention to the STAT column, holding the current state of a process. Critically, T stands for “stopped by a job control signal”, which means it’s a stopped job of a terminal.

The output above shows that we have stopped process 5767 while process 5768 is still running. Now, we use the awk language to filter only those jobs:

$ tty /dev/pts/1 $ ps aux | awk 5767 T sleep 1m 

Above, we only use rows of output, where column 8 ($8, STAT) has a value of T. From those rows, we extract the process ID, STAT, and actual command run via $2″ “$8” “$11. Of course, it’s easy to only print $2 for the PID. This awk expression will show us all the stopped jobs as a list.

Читайте также:  Линукс рекурсивно задать права

Finally, we can kill all processes of interest by piping our output from ps to kill:

$ tty /dev/pts/1 $ kill -9 `ps aux | awk ` 

We can verify the correct processes have been killed in the original terminal:

$ tty /dev/pts/0 $ jobs [1]+ Killed sleep 1m [2]- Running sleep 2m & 

As the jobs command tells us, we have successfully killed only the stopped processes in another terminal.

4. Conclusion

In this article, we briefly covered how to kill all stopped jobs under different circumstances. First, we used the jobs command to identify local terminal processes. After that, we employed ps to help with specific cases.

Источник

There are stopped jobs (on bash exit)

Bash did not exit, I must exit again to exit the bash shell.

  • Q: What is a 'stopped job', or what does this mean?
  • Q: Can a stopped process be resumed?
  • Q: Does the first exit kill the stopped jobs?
  • Q: Is there a way to exit the shell the first time? (without entering exit twice)

2 Answers 2

A stopped job is one that has been temporarily put into the background and is no longer running, but is still using resources (i.e. system memory). Because that job is not attached to the current terminal, it cannot produce output and is not receiving input from the user.

You can see jobs you have running using the jobs builtin command in bash, probably other shells as well. Example:

user@mysystem:~$ jobs [1] + Stopped python user@mysystem:~$ 

You can resume a stopped job by using the fg (foreground) bash built-in command. If you have multiple commands that have been stopped you must specify which one to resume by passing jobspec number on the command line with fg . If only one program is stopped, you may use fg alone:

At this point you are back in the python interpreter and may exit by using control-D.

Conversely, you may kill the command with either it's jobspec or PID. For instance:

user@mysystem:~$ ps PID TTY TIME CMD 16174 pts/3 00:00:00 bash 17781 pts/3 00:00:00 python 18276 pts/3 00:00:00 ps user@mysystem:~$ kill 17781 [1]+ Killed python user@mysystem:~$ 

To use the jobspec, precede the number with the percent (%) key:

user@mysystem:~$ kill %1 [1]+ Terminated python 

If you issue an exit command with stopped jobs, the warning you saw will be given. The jobs will be left running for safety. That's to make sure you are aware you are attempting to kill jobs you might have forgotten you stopped. The second time you use the exit command the jobs are terminated and the shell exits. This may cause problems for some programs that aren't intended to be killed in this fashion.

In bash it seems you can use the logout command which will kill stopped processes and exit. This may cause unwanted results.

Also note that some programs may not exit when terminated in this way, and your system could end up with a lot of orphaned processes using up resources if you make a habit of doing that.

Note that you can create background process that will stop if they require user input:

user@mysystem:~$ python & [1] 19028 user@mysystem:~$ jobs [1]+ Stopped python 

You can resume and kill these jobs in the same way you did jobs that you stopped with the Ctrl-z interrupt.

Источник

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