2

A while ago my teacher asked me to create a script that would kill Minecraft anytime it came up. It worked when the user themselves ran it, but it didn't if it was at root level.

Is there a command similar to lsof that found all processes, not just ones by the user that called lsof? I read the man page, but I'm not quite understanding it. Is there a lsof command that will get every single process, not just the users'?

This is the script, designed only for Macs:

#!/usr/bin/env python
import os
from time import sleep

while True:
    os.system("lsof | grep minecraft | cut -c 11-16 >mine.txt")
    f=open("mine.txt")
    l = f.readline()
    if len(l) > 0:
        os.system("kill "+l)
    f.close()
    os.system("echo > mine.txt")
    sleep(15)
nc4pk
  • 9,037
  • 14
  • 59
  • 71
Hovestar
  • 145
  • 9
  • Hey could I get my hands on that script? I could use it. I'd also like to expand it's capabilities so source code would be nice. – Griffin Mar 09 '13 at 02:51
  • 1
    @Griffin Sure I'll edit into the question. It's fairly simple. – Hovestar Mar 09 '13 at 02:54
  • Actually that's an even better idea. Showing them the source might give them a better understand of what to change. Sadly I can not help because I'd don't know but other who do might find it useful. – Griffin Mar 09 '13 at 02:55
  • 1
    why are you using `lsof`? it's designed to list open files. i think it'd be better to use something like `ps`. and as for listing the processes of all the users... if memory serves, that's impossible, as it's a potential security issue – nc4pk Mar 09 '13 at 03:04
  • :( Me no know python. How you do in C++ – Griffin Mar 09 '13 at 03:09
  • I know a little ANSII C but not C++ I'll figure it out real quick then I'll get back to you, but unless the user themselves activates the code it's useless at this point. – Hovestar Mar 09 '13 at 03:13
  • So I couldn't find a way of making terminal execute commands from c++, but if you could, and it sends the command lsof | grep minecraft | cut -c 11-16 >mine.txt then it reads from mine.txt and gets the first line, which is the pid, it then kills that pid. – Hovestar Mar 09 '13 at 03:42

2 Answers2

2

This prints the names of all processes:

ps -axco comm | sed 1d
  • -a: all users (like root when run as user and user when run as root)
  • -x: include processes without a controlling terminal
  • -o comm: output only the command column
  • -c: use command names instead of paths in the command column
  • sed 1d: delete the first line (COMM)

If you only want to quit Minecraft, you could run a shell script like this (or just killall minecraft with launchd or cron):

while sleep 5; do killall minecraft 2> /dev/null; done
Lri
  • 40,894
  • 7
  • 119
  • 157
1

a command similar to lsof that scan all processes, not just ones by the user that called lsof

pgrep -f minecraft

Where minecraft is the process to find. The command returns list of PIDs.

Here is the kill command (shell syntax):

kill $(pgrep -f minecraft)

Instead of using Python, here is a simple shell command to achieve the same:

while true; do echo kill $(pgrep -f minecraft) 2> /dev/null; sleep 15; done
kenorb
  • 24,736
  • 27
  • 129
  • 199