socko2: kill process by name

  • i know a process can be killed by name, since ProcessViewer can filter by
    name and kill a process.  does anyone know how to (in Cocoa):

    1.  find a process by name
    2.  kill that process

    i have been able to use the process name to get the process ID of the
    process i want to kill in Terminal, but i don't know how to get the
    process ID into a kill command once the process ID has been listed in
    Terminal.  i'm hoping there is some way to do it using Obj-C so i can
    avoid Terminal altogether.
    Send FREE Valentine eCards with Yahoo! Greetings!
    http://greetings.yahoo.com
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Dave,

    thanks, but again:

    >> i'm hoping there is some way to do it using Obj-C so i can
    >> avoid Terminal altogether.

    tcsh assumes Terminal implementation, doesn't it?  i want to avoid using
    Terminal.

    i see that NSTask allows the opening (and termination) of a subprocess,
    but don't we first need to know how to id the process?  in my case, i want
    to terminate a process that is already running.

    am i missing something?

    --- David Remahl <chmod007...> wrote:
    >> i know a process can be killed by name, since ProcessViewer can filter
    > by
    >> name and kill a process.  does anyone know how to (in Cocoa):
    >>
    >> 1.  find a process by name
    >> 2.  kill that process
    >>
    >> i have been able to use the process name to get the process ID of the
    >> process i want to kill in Terminal, but i don't know how to get the
    >> process ID into a kill command once the process ID has been listed in
    >> Terminal.  i'm hoping there is some way to do it using Obj-C so i can
    >> avoid Terminal altogether.
    >> Send FREE Valentine eCards with Yahoo! Greetings!
    >> http://greetings.yahoo.com
    >> _______________________________________________
    >> cocoa-dev mailing list | <cocoa-dev...>
    >> Help/Unsubscribe/Archives:
    >> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    >> Do not post admin requests to the list. They will be ignored.
    >
    > I use this tcsh alias to be able to do things like "> pkill Finder":
    >
    > alias pkill 'kill `ps -x | grep \!^ | cut -c2-6 | head -n 1`'
    >
    > You can use that to adjust to your requirements...NSTask and NSPipe are
    > the
    > obj-C classes you will need...
    >
    > / david
    Send FREE Valentine eCards with Yahoo! Greetings!
    http://greetings.yahoo.com
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Wednesday, February 6, 2002, at 08:39 PM, socko wrote:

    > i see that NSTask allows the opening (and termination) of a subprocess,
    > but don't we first need to know how to id the process?  in my case, i
    > want
    > to terminate a process that is already running.
    >
    > am i missing something?

    He told you how to get the pid of the process using the terminal. If you
    want to get it programmatically, man 3 sysctl, and to terminate it, man
    2 kill.

      -- Finlay
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Socko,

    You should be able to use NSTask to call "ps -all" to list the processes
    and get the id, then use NSTask to call "kill id" (where id is the
    actual id of the process) to kill the process. Mind you, without the
    visual feedback that you get when you run ps in the Terminal, it might
    be hard to be SURE which process you're killing.

    Regards,

    Jake

    On Thursday, February 7, 2002, at 06:16  AM, socko wrote:

    > i know a process can be killed by name, since ProcessViewer can filter
    > by
    > name and kill a process.  does anyone know how to (in Cocoa):
    >
    > 1.  find a process by name
    > 2.  kill that process
    >
    > i have been able to use the process name to get the process ID of the
    > process i want to kill in Terminal, but i don't know how to get the
    > process ID into a kill command once the process ID has been listed in
    > Terminal.  i'm hoping there is some way to do it using Obj-C so i can
    > avoid Terminal altogether.
    > Send FREE Valentine eCards with Yahoo! Greetings!
    > http://greetings.yahoo.com
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • I had to do something similar in one of my projects... It is quite
    simple using the Carbon Process Manager API.

    You can go through the list of processes and find the specific one. If
    it is running, you can then send an appleevent
    AEQuit to that process.

    Alex

    >
    >
    > On Thursday, February 7, 2002, at 06:16  AM, socko wrote:
    >
    >> i know a process can be killed by name, since ProcessViewer can filter
    >> by
    >> name and kill a process.  does anyone know how to (in Cocoa):
    >>
    >> 1.  find a process by name
    >> 2.  kill that process
    >>
    >> i have been able to use the process name to get the process ID of the
    >> process i want to kill in Terminal, but i don't know how to get the
    >> process ID into a kill command once the process ID has been listed in
    >> Terminal.  i'm hoping there is some way to do it using Obj-C so i can
    >> avoid Terminal altogether.
    >> Send FREE Valentine eCards with Yahoo! Greetings!
    >> http://greetings.yahoo.com
    >> _______________________________________________
    >> cocoa-dev mailing list | <cocoa-dev...>
    >> Help/Unsubscribe/Archives:
    >> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    >> Do not post admin requests to the list. They will be ignored.
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • You can walk the process list using UNIX calls (see "man 3 sysctl" and
    "man 2 kill").

        int mib[ 3 ] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
        size_t length;
        struct kinfo_proc *theProcessInfoData;
        struct kinfo_proc *theProcessInfo;
        NSString *theProcessName;
        pid_t theProcessID;

        if( sysctl( mib, 3, NULL, &length, NULL, 0 ) == -1 ) return nil;

        theProcessInfoData = (struct kinfo_proc*)malloc( length );

        if( sysctl( mib, 3, theProcessInfoData, &length, NULL, 0 ) != -1 ) {
            theProcessInfo = theProcessInfoData;

            do {
                theProcessName = [NSString
    stringWithCString:theProcessInfo -> kp_proc.p_comm];
                theProcessID = theProcessInfo -> kp_proc.p_pid;

                fprintf( stderr, "%s = %d\n", [theProcessName cString],
    (int)theProcessID );

                theProcessInfo ++;
            } while( (size_t)theProcessInfo < (size_t)theProcessInfoData +
    length );
        }

        free( (void*)theProcessInfoData );

    ---
    Only Mortal, the internet game server browser for MacOS X.
    Visit: http://homepage.mac.com/only_mortal/
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month february 2002 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28      
Go to today
MindNode
MindNode offered a free license !