2

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?

Vincent
  • 580
  • Is there a particular process you want to deal with. Do you want to kill the process or prevent it stating? – mmmmmm Aug 23 '18 at 11:04
  • @Mark There is no a particular process. Preventing it from starting would be better. – Vincent 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 – mmmmmm Aug 23 '18 at 22:21

1 Answers1

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 
user7886229
  • 10,009
  • 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 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 – user7886229 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 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). – user7886229 Aug 23 '18 at 11:28
  • Cool. That would be grateful. Please take you time. No hurries. – Vincent Aug 23 '18 at 11:31
  • @user174782 How often do you want to check if the process is open? (Unfortunately, I can do a listener so we have to start on an interval). 5 seconds would give the user access to the application a max of 5 seconds before kill the process. We would run it 24/7 if you would like. – user7886229 Aug 23 '18 at 11:46
  • Can we give it a 2 seconds and 24/7? – Vincent Aug 23 '18 at 11:49
  • @user174782 See if that works. – user7886229 Aug 23 '18 at 13:01
  • Great job, Josh. But you have to start the script (app) manually every time the Mac starts up. And you are not able to shut down or restart your Mac, unless you force quit the app (script) manually. I wonder if there is any way to put the script to a plist. I'm not sure if you can get me. As we know, you can load a plist automatically even the Mac reboots, but an app (script) doesn't. Even you can restarts an app (script), but you can not shut down your Mac, just because the app (created with Apple Script) won't quit automatically after you click  - Shut Down or Restart... – Vincent Aug 23 '18 at 13:27
  • Hi Josh, could you please post the original answer (The one which is used to determine which process is running and quit the process after it found running) here again? Thanks. – Vincent Aug 23 '18 at 19:49
  • @user174782 This one? – user7886229 Aug 23 '18 at 19:52
  • Sorry, I remember it's a bash script. – Vincent Aug 23 '18 at 19:54
  • @user174782 Go to the edit menu and tell me the edit number or suggest it and ill accept it. – user7886229 Aug 23 '18 at 19:57
  • I just saw the revision history. Now everything is ok. Thanks, Josh. – Vincent Aug 23 '18 at 20:02
  • @user174782 Rolling back to bash I like it better – user7886229 Aug 23 '18 at 20:05