How to kill a process on Mac programatically

Discussion in 'Mac Programming' started by satyam90, Nov 5, 2008.

  1. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #1
    I am working with Obj C and Cocoa framework.
    I am working on an application which has UI that interacts with a background process (Both are written by me). When application is started, background process is also started. Here, I want to put a check that if background process is already running (by any chance or if UI is crashed) it must be killed and new process must be started.
    What is the way to know whether a particular process is running or not and how to kill it programatically.
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    You should be able to use AppleEvents or signal() to terminate a process.
     
  3. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #3
    Read the man pages for kill(2) sigaction(2) and signal(3)
    THese are all pportable and work on any unix-like system

    But if your process creates the others then it will get a signal when they terminate so you can avoid the checking and avoid the lag time between when it dies and when you check. You would know the process ID of the other tasks.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    You should also check out NDProcess which is a Cocoa wrapper around the Carbon Process Manager functions.
     
  5. satyam90 thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #5
    Killing a process programatically

    I wrote an application in Obj C and Cocoa framework.
    Before starting my application, I want to make sure that some processes (my other C++ process) are not running. If running, I have to terminate using some programming inside my application itself.
    I don't want to NSTask to use "ps -ea | grep................" and kill the process.
    Is there any way to do it by programming Obj C and Cocoa.
    --Satyam.
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Merging with your existing thread

    Snow Leopard added a new NSRunningApplication class which has a forceTerminate and terminate method, if you don't need backwards compatibility.
     
  7. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #7
    The "old way" that has been used for 25+ years on most Unix systems is then you start a process that you later might need to kill you also create a file in /var/programname/ that uses the process ID as the file name. Then later you loop over those files killing the processes (see man page kill(2)) and deleting (unlinking) the files. It is simple enough that this is many times done in a shell script. This is portable and works for C/C++ process that operate outside of Apple's APIs
     

Share This Page