How do I kill the last spawned background task in Linux?
Example:
doSomething doAnotherThing doB & doC doD #kill doB ???? How do I kill the last spawned background task in Linux?
Example:
doSomething doAnotherThing doB & doC doD #kill doB ???? You can kill by job number. When you put a task in the background you'll see something like:
$ ./script & [1] 35341 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 numbernmay 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,%cerefers to a stoppedcejob. If a prefix matches more than one job, bash reports an error. Using%?ce, on the other hand, refers to any job containing the stringcein 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.
set -m), it's intended for interactive use. See stackoverflow.com/questions/690266/… as well%1 and %% - especially. Some things don't die on Ctrl-C, so you need to Ctrl-Z them, and then kill -9 %%. One example where I found it useful is: while true; do mplayer <some unstable online radio>; date >> restarts.log; done - Ctrl-C will just get you to next loop iteration. Before I had to do ps or maybe jobs -l, and then re-type the PID, which is tedious.while true with while sleep 1. This will give you a short delay before restarts if you can live with that, and if you hit ctrl-c twice, the second one will interrupt the sleep, ending it with a non-zero exit and breaking out of the loop.kill: failed to parse argument: '%1'" (e.g., for sudo kill %1 after Ctrl + Z in, for example, sudo less /media/someUser/364c375a-523c-41ae-b858-0fa9774540a3/grub/grub.cfg). kill %1 (without sudo) may give the desired outcome, but with an error message: "bash: kill: (23076) - Operation not permitted. and [1]+ Stopped sudo less /media/someUser/364c375a-523c-41ae-b858-0fa9774540a3/grub/grub.cfg"There's a special variable for this in Bash:
kill $! $! expands to the PID of the last process executed in the background.
^Z doesn't background jobs, it stops them. A subsequent bg does the actual 'backgrounding' (resumes execution in the background), and after that $! works as expected.???? stands for one or more commands to be executed after the kill, if any of those commands relies on work done by the background process, be mindful of any cleanup or finishing-up tasks which the background process might perform in a signal handler after receiving a (trappable) signal. Best to add a wait (followed perhaps by a sync or even a sleep <n>) right before the first of any such 'dependent' commands.This should kill all background processes:
jobs -p | xargs kill -9 kill -9 %% is less typing :)As in John Kugelman's answer, % is related to job specification.
How can we efficiently find that? Use less's &pattern command. It seems man uses the less pager (I am not that sure). In 'man' Bash, type &%, and then type Enter. It will only show lines that containing '%'. To reshow all, type &. And then Enter.
Just use the killall command:
killall taskname
for more info and more advanced options, type "man killall".
killall python or killall java, while having something useful running elswhere in the system.