bash kill process by call Announcing the arrival of Valued Associate #679: Cesar Manara ...

How much damage would a cupful of neutron star matter do to the Earth?

What is the difference between a "ranged attack" and a "ranged weapon attack"?

How does Belgium enforce obligatory attendance in elections?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Karn the great creator - 'card from outside the game' in sealed

How do I find out the mythology and history of my Fortress?

In musical terms, what properties are varied by the human voice to produce different words / syllables?

Why are vacuum tubes still used in amateur radios?

Amount of permutations on an NxNxN Rubik's Cube

Sum letters are not two different

One-one communication

Dyck paths with extra diagonals from valleys (Laser construction)

What is the chair depicted in Cesare Maccari's 1889 painting "Cicerone denuncia Catilina"?

Drawing spherical mirrors

How do living politicians protect their readily obtainable signatures from misuse?

How can I set the aperture on my DSLR when it's attached to a telescope instead of a lens?

Trademark violation for app?

What does 丫 mean? 丫是什么意思?

Dynamic filling of a region of a polar plot

What does Turing mean by this statement?

As Singapore Airlines (Krisflyer) Gold, can I bring my family into the lounge on a domestic Virgin Australia flight?

Do wooden building fires get hotter than 600°C?

How many morphisms from 1 to 1+1 can there be?

How to compare two different files line by line in unix?



bash kill process by call



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionGraceful way to kill processWhy can't I kill a timeout called from a Bash script with a keystroke?Host process for multiple processes?How can I create a hard to kill processWhy does kill not have a parameter for file and not for port number?kill xterm process in a bashCatch the kill signal when a process is in S state [interruptible sleep]writing a script & trying to kill a processStart python http server which can be stopped by anyone elseCan I make a process become a job of a bash process?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I am using bash to start several instances of one python program, like this:



python example.py -id $i


where $i is the id given to the instance. All the processes are started on the same user, and imagine I am unable of keeping track of the process IDs. Would it be possible to kill the specific instance just by the command (with id) that called it? For example something like pkill 'python example.py -id 2 which obviously doesn't work.










share|improve this question
















bumped to the homepage by Community 2 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 4





    Try pkill -f python example.py -id 2. The man page for pkill explains the -f flag (see man pkill). And generally, it's a good idea to quote your variables ("$i" instead of just $i).

    – roaima
    Jul 25 '16 at 21:42













  • If the processes are running under the current shell, then the jobs command and then kill %2 or whatnot may be of use, to use the shell job control functionality.

    – thrig
    Jul 25 '16 at 22:21


















1















I am using bash to start several instances of one python program, like this:



python example.py -id $i


where $i is the id given to the instance. All the processes are started on the same user, and imagine I am unable of keeping track of the process IDs. Would it be possible to kill the specific instance just by the command (with id) that called it? For example something like pkill 'python example.py -id 2 which obviously doesn't work.










share|improve this question
















bumped to the homepage by Community 2 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 4





    Try pkill -f python example.py -id 2. The man page for pkill explains the -f flag (see man pkill). And generally, it's a good idea to quote your variables ("$i" instead of just $i).

    – roaima
    Jul 25 '16 at 21:42













  • If the processes are running under the current shell, then the jobs command and then kill %2 or whatnot may be of use, to use the shell job control functionality.

    – thrig
    Jul 25 '16 at 22:21














1












1








1








I am using bash to start several instances of one python program, like this:



python example.py -id $i


where $i is the id given to the instance. All the processes are started on the same user, and imagine I am unable of keeping track of the process IDs. Would it be possible to kill the specific instance just by the command (with id) that called it? For example something like pkill 'python example.py -id 2 which obviously doesn't work.










share|improve this question
















I am using bash to start several instances of one python program, like this:



python example.py -id $i


where $i is the id given to the instance. All the processes are started on the same user, and imagine I am unable of keeping track of the process IDs. Would it be possible to kill the specific instance just by the command (with id) that called it? For example something like pkill 'python example.py -id 2 which obviously doesn't work.







command-line process kill






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 25 '16 at 21:56









Gilles

549k13011161631




549k13011161631










asked Jul 25 '16 at 21:27









Jeroen SmuldersJeroen Smulders

61




61





bumped to the homepage by Community 2 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 4





    Try pkill -f python example.py -id 2. The man page for pkill explains the -f flag (see man pkill). And generally, it's a good idea to quote your variables ("$i" instead of just $i).

    – roaima
    Jul 25 '16 at 21:42













  • If the processes are running under the current shell, then the jobs command and then kill %2 or whatnot may be of use, to use the shell job control functionality.

    – thrig
    Jul 25 '16 at 22:21














  • 4





    Try pkill -f python example.py -id 2. The man page for pkill explains the -f flag (see man pkill). And generally, it's a good idea to quote your variables ("$i" instead of just $i).

    – roaima
    Jul 25 '16 at 21:42













  • If the processes are running under the current shell, then the jobs command and then kill %2 or whatnot may be of use, to use the shell job control functionality.

    – thrig
    Jul 25 '16 at 22:21








4




4





Try pkill -f python example.py -id 2. The man page for pkill explains the -f flag (see man pkill). And generally, it's a good idea to quote your variables ("$i" instead of just $i).

– roaima
Jul 25 '16 at 21:42







Try pkill -f python example.py -id 2. The man page for pkill explains the -f flag (see man pkill). And generally, it's a good idea to quote your variables ("$i" instead of just $i).

– roaima
Jul 25 '16 at 21:42















If the processes are running under the current shell, then the jobs command and then kill %2 or whatnot may be of use, to use the shell job control functionality.

– thrig
Jul 25 '16 at 22:21





If the processes are running under the current shell, then the jobs command and then kill %2 or whatnot may be of use, to use the shell job control functionality.

– thrig
Jul 25 '16 at 22:21










2 Answers
2






active

oldest

votes


















0














kill $(ps -ef | grep '[s]earch-string' | sed -re 's/[[:blank:]]+/t/g' | cut -f2)





  1. ps -ef : gets process list with arguments


  2. grep : filters for the record you want the [] is to stop grep finding its self.


  3. sed -re 's/[[:blank:]]+/t/g' : converts space to a single tab


  4. cut -f2 : extracts the pid field






share|improve this answer



















  • 1





    @roaima's comment may be a simpler answer.

    – ctrl-alt-delor
    Jul 25 '16 at 21:48











  • Not really portable: ps: unknown option -- f usage: ps [-AaceHhjkLlmrSTuvwx] [-M core] [-N system] [-O fmt] [-o fmt] [-p pid] [-t tty] [-U username] [-W swap]

    – Kusalananda
    Jul 30 '16 at 13:14











  • ps is not very portable, there is one for posix, one for bsd, one for system-v. The Gnu one looks like it tries to be compatible with all. If you want help with ps then you will have to tell us which OS and version of ps.

    – ctrl-alt-delor
    Aug 11 '16 at 16:25



















0














Using pkill:



pkill -fx 'python example.py -id 2'


The -f option to pkill makes matching happen against the full command line, not just the process name, and -x forces the pattern to match the full command exactly, not just any substring of it (similarly as with grep -x). You'll need -x to avoid matching command lines that possibly had further digits at the end, like 20 or 203.






share|improve this answer


























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f298248%2fbash-kill-process-by-call%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    kill $(ps -ef | grep '[s]earch-string' | sed -re 's/[[:blank:]]+/t/g' | cut -f2)





    1. ps -ef : gets process list with arguments


    2. grep : filters for the record you want the [] is to stop grep finding its self.


    3. sed -re 's/[[:blank:]]+/t/g' : converts space to a single tab


    4. cut -f2 : extracts the pid field






    share|improve this answer



















    • 1





      @roaima's comment may be a simpler answer.

      – ctrl-alt-delor
      Jul 25 '16 at 21:48











    • Not really portable: ps: unknown option -- f usage: ps [-AaceHhjkLlmrSTuvwx] [-M core] [-N system] [-O fmt] [-o fmt] [-p pid] [-t tty] [-U username] [-W swap]

      – Kusalananda
      Jul 30 '16 at 13:14











    • ps is not very portable, there is one for posix, one for bsd, one for system-v. The Gnu one looks like it tries to be compatible with all. If you want help with ps then you will have to tell us which OS and version of ps.

      – ctrl-alt-delor
      Aug 11 '16 at 16:25
















    0














    kill $(ps -ef | grep '[s]earch-string' | sed -re 's/[[:blank:]]+/t/g' | cut -f2)





    1. ps -ef : gets process list with arguments


    2. grep : filters for the record you want the [] is to stop grep finding its self.


    3. sed -re 's/[[:blank:]]+/t/g' : converts space to a single tab


    4. cut -f2 : extracts the pid field






    share|improve this answer



















    • 1





      @roaima's comment may be a simpler answer.

      – ctrl-alt-delor
      Jul 25 '16 at 21:48











    • Not really portable: ps: unknown option -- f usage: ps [-AaceHhjkLlmrSTuvwx] [-M core] [-N system] [-O fmt] [-o fmt] [-p pid] [-t tty] [-U username] [-W swap]

      – Kusalananda
      Jul 30 '16 at 13:14











    • ps is not very portable, there is one for posix, one for bsd, one for system-v. The Gnu one looks like it tries to be compatible with all. If you want help with ps then you will have to tell us which OS and version of ps.

      – ctrl-alt-delor
      Aug 11 '16 at 16:25














    0












    0








    0







    kill $(ps -ef | grep '[s]earch-string' | sed -re 's/[[:blank:]]+/t/g' | cut -f2)





    1. ps -ef : gets process list with arguments


    2. grep : filters for the record you want the [] is to stop grep finding its self.


    3. sed -re 's/[[:blank:]]+/t/g' : converts space to a single tab


    4. cut -f2 : extracts the pid field






    share|improve this answer













    kill $(ps -ef | grep '[s]earch-string' | sed -re 's/[[:blank:]]+/t/g' | cut -f2)





    1. ps -ef : gets process list with arguments


    2. grep : filters for the record you want the [] is to stop grep finding its self.


    3. sed -re 's/[[:blank:]]+/t/g' : converts space to a single tab


    4. cut -f2 : extracts the pid field







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 25 '16 at 21:46









    ctrl-alt-delorctrl-alt-delor

    12.4k52663




    12.4k52663








    • 1





      @roaima's comment may be a simpler answer.

      – ctrl-alt-delor
      Jul 25 '16 at 21:48











    • Not really portable: ps: unknown option -- f usage: ps [-AaceHhjkLlmrSTuvwx] [-M core] [-N system] [-O fmt] [-o fmt] [-p pid] [-t tty] [-U username] [-W swap]

      – Kusalananda
      Jul 30 '16 at 13:14











    • ps is not very portable, there is one for posix, one for bsd, one for system-v. The Gnu one looks like it tries to be compatible with all. If you want help with ps then you will have to tell us which OS and version of ps.

      – ctrl-alt-delor
      Aug 11 '16 at 16:25














    • 1





      @roaima's comment may be a simpler answer.

      – ctrl-alt-delor
      Jul 25 '16 at 21:48











    • Not really portable: ps: unknown option -- f usage: ps [-AaceHhjkLlmrSTuvwx] [-M core] [-N system] [-O fmt] [-o fmt] [-p pid] [-t tty] [-U username] [-W swap]

      – Kusalananda
      Jul 30 '16 at 13:14











    • ps is not very portable, there is one for posix, one for bsd, one for system-v. The Gnu one looks like it tries to be compatible with all. If you want help with ps then you will have to tell us which OS and version of ps.

      – ctrl-alt-delor
      Aug 11 '16 at 16:25








    1




    1





    @roaima's comment may be a simpler answer.

    – ctrl-alt-delor
    Jul 25 '16 at 21:48





    @roaima's comment may be a simpler answer.

    – ctrl-alt-delor
    Jul 25 '16 at 21:48













    Not really portable: ps: unknown option -- f usage: ps [-AaceHhjkLlmrSTuvwx] [-M core] [-N system] [-O fmt] [-o fmt] [-p pid] [-t tty] [-U username] [-W swap]

    – Kusalananda
    Jul 30 '16 at 13:14





    Not really portable: ps: unknown option -- f usage: ps [-AaceHhjkLlmrSTuvwx] [-M core] [-N system] [-O fmt] [-o fmt] [-p pid] [-t tty] [-U username] [-W swap]

    – Kusalananda
    Jul 30 '16 at 13:14













    ps is not very portable, there is one for posix, one for bsd, one for system-v. The Gnu one looks like it tries to be compatible with all. If you want help with ps then you will have to tell us which OS and version of ps.

    – ctrl-alt-delor
    Aug 11 '16 at 16:25





    ps is not very portable, there is one for posix, one for bsd, one for system-v. The Gnu one looks like it tries to be compatible with all. If you want help with ps then you will have to tell us which OS and version of ps.

    – ctrl-alt-delor
    Aug 11 '16 at 16:25













    0














    Using pkill:



    pkill -fx 'python example.py -id 2'


    The -f option to pkill makes matching happen against the full command line, not just the process name, and -x forces the pattern to match the full command exactly, not just any substring of it (similarly as with grep -x). You'll need -x to avoid matching command lines that possibly had further digits at the end, like 20 or 203.






    share|improve this answer






























      0














      Using pkill:



      pkill -fx 'python example.py -id 2'


      The -f option to pkill makes matching happen against the full command line, not just the process name, and -x forces the pattern to match the full command exactly, not just any substring of it (similarly as with grep -x). You'll need -x to avoid matching command lines that possibly had further digits at the end, like 20 or 203.






      share|improve this answer




























        0












        0








        0







        Using pkill:



        pkill -fx 'python example.py -id 2'


        The -f option to pkill makes matching happen against the full command line, not just the process name, and -x forces the pattern to match the full command exactly, not just any substring of it (similarly as with grep -x). You'll need -x to avoid matching command lines that possibly had further digits at the end, like 20 or 203.






        share|improve this answer















        Using pkill:



        pkill -fx 'python example.py -id 2'


        The -f option to pkill makes matching happen against the full command line, not just the process name, and -x forces the pattern to match the full command exactly, not just any substring of it (similarly as with grep -x). You'll need -x to avoid matching command lines that possibly had further digits at the end, like 20 or 203.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 20 at 18:20

























        answered Mar 20 at 18:14









        KusalanandaKusalananda

        142k18266442




        142k18266442






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f298248%2fbash-kill-process-by-call%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...