1

What I'm trying to do is: kill a process when it starts up. So I would like to find out that how to detect when a process starts up? Or how to block a process or an application from starting up?

Any suggestions would be appreciated?

  • Is there a particular process you want to deal with. Do you want to kill the process or prevent it stating? – Mark Aug 23 '18 at 11:04
  • @Mark There is no a particular process. Preventing it from starting would be better. – Vincent Sun Aug 23 '18 at 11:06
  • The obvious thing to do is delete the executable - This seems like an XY problem, what are you trying to do. Some processes are restarted automatically if they are killed for many killing after 5 seconds (as in answer) is too late they have done the harm. It is better to attack the problem in another way – Mark Aug 23 '18 at 22:21
1

Create the App

Open this in Script Editor and export it as a read-only application and make sure stay open after run handler is off

After exporting follow this guide to prevent it from show up in the dock.

# Block Apps By Josh Brown
# Last Modified: Aug 23 2018
global applist

on run
    set applist to {"Google Chrome", "App Store"} -- Apps to limit
    if checkapps() then
        killall()
    end if
end run
on is_running(appName)
    try
        if (the length of (do shell script "pgrep -x " & quoted form of appName) > 0) then
            kill(do shell script "pgrep -x " & quoted form of appName)
        end if
    end try
end is_running

on checkapps()
    set x to false
    repeat with a from 1 to length of applist
        is_running(item a of applist)
    end repeat
    return x
end checkapps

on kill(theID)
    do shell script "kill -9 " & theID
end kill

Create the LaunchDaemon

Note: You must be an admin to do this.

Save the following file to /Library/LaunchDaemons/

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.PlzUpvoteMy.answer</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/bin/open</string>
            <string>-W</string>
            <string>**/path/to/application.app**</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>5</integer>
        <key>UserName</key>
        <string>**UserToBlock**</string>
    </dict>
</plist>

Change the permissions with the following command:

sudo chown root:wheel /Library/LaunchDaemons/com.MyName.plist

Load the Daemon

Note: You must be admin to do this.

To start the daemon use this command:

sudo launchctl load /Library/LaunchDaemons/com.MyName.plist 

The program will scan for the apps every 5 seconds and close them if they are running.

To stop the Daemon use this command

sudo launchctl unload /Library/LaunchDaemons/com.MyName.plist 
  • Thanks, @JBis . I'm not trying to figure out which process is running. I'm trying to prevent a process from running or starting. If it's impossible, I'm trying to find out how to kill a process once it starts up. – Vincent Sun Aug 23 '18 at 11:13
  • @user174782 Please check the link. If you set time limit to 0/alter code to your needs it will do exactly what your looking for – JBis Aug 23 '18 at 11:14
  • Very nice work, @JBis. Any way to keep it in the background? So that when the system starts up, the script will start up. And adding it to the user login items is not a good idea, because people will remove it, you know. – Vincent Sun Aug 23 '18 at 11:26
  • @user174782 Yes. Actually I should have thought of that before. I will add that part in about an hour (on mobile now). – JBis Aug 23 '18 at 11:28
  • Cool. That would be grateful. Please take you time. No hurries. – Vincent Sun Aug 23 '18 at 11:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .