Sublime Forum

Kill previous process when building

#1

I’ve just started using Sublime and love the experience so far, but for development, the fact that I need to manually kill the process after each time running a build (-B) is dreadful.

I tried making a custom build script like this:

{
	"cmd":  "kill `ps -ef | grep $file_name | grep -v grep | awk '{print $2}'` || true && python3", "$file"],
	"selector": "main.py",
	"path": "/usr/local/bin"
}

which did not work out as I wanted it to, is there a simple way to do it? Googling turned up nothing of use.

0 Likes

#2

I am also interested in this issue.
I would like to run my node.js apps via the build system but I’m getting EADDRINUSE because the previous process is not terminated.

Thanks in advance for any help.

0 Likes

#3

I’m interested in this too.

I tried the below but it kills all the instances just as they start which is odd as its the first command to execute.

"build_systems":

	{
		"name": "NodeF1Build",
		"shell": true,
		"cmd": "killall node & nodemon --debug $project_path/app.js & node-inspector"], 
		
			"file_regex": "^ ]*File \"(...*?)\", line ([0-9]*)",
			"selector": "source.js"
	}

]
0 Likes

#4

Is there no progress here? Killing all node processes indiscriminately prior to building isn’t a very good solution. Currently can’t run any sort of debugger or inspector for my node builds.

0 Likes

#5

I’m a little bit lost here, are you guys using ExecCommand to run your build_systems? Because if it’s so such command already implements the kill action, i.e:

def kill(self):
    if not self.killed:
        self.killed = True
        if sys.platform == "win32":
            # terminate would not kill process opened by the shell cmd.exe,
            # it will only kill cmd.exe leaving the child running
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            subprocess.Popen(
                "taskkill /PID " + str(self.proc.pid),
                startupinfo=startupinfo)
        else:
            self.proc.terminate()
        self.listener = None

The python spawned process is killed in a multiplatform way, if I’m not mistaken this method is being called when using Tools\Cancel build (ctrl+break).

A while ago I had faced something similar, some build_systems were using my own ExecCommand subclass so my solution had been adding something like this:

class ZebraVirtualenvCancelCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.run_command("zebra_virtualenv_exec", {"kill": True})

    def is_enabled(self):
        return True

That way I could bind this command to a key shortcut and killing my process spawned by my own ExecCommand subclass. In any case, as I’ve said, if you’re using the default ExecCommand (default command) just use its own implementation through Cancel build, which is more than good enough for a big number of cases.

0 Likes