mirror of
https://github.com/ollama/ollama
synced 2026-04-23 08:45:14 +00:00
Merge e899ea7ccf into 160660e572
This commit is contained in:
commit
f572d53789
|
|
@ -46,7 +46,22 @@ func terminated(pid int) (bool, error) {
|
|||
return false, nil
|
||||
}
|
||||
|
||||
// reapServers kills all ollama processes except our own
|
||||
func ollamaServeProcess(pid int) bool {
|
||||
output, err := exec.Command("ps", "-p", strconv.Itoa(pid), "-o", "args=").Output()
|
||||
if err != nil {
|
||||
slog.Debug("failed to inspect ollama process", "pid", pid, "err", err)
|
||||
return false
|
||||
}
|
||||
|
||||
args := strings.Fields(strings.TrimSpace(string(output)))
|
||||
if len(args) < 2 {
|
||||
return false
|
||||
}
|
||||
|
||||
return filepath.Base(args[0]) == "ollama" && args[1] == "serve"
|
||||
}
|
||||
|
||||
// reapServers kills external ollama serve processes except our own.
|
||||
func reapServers() error {
|
||||
// Get our own PID to avoid killing ourselves
|
||||
currentPID := os.Getpid()
|
||||
|
|
@ -82,6 +97,10 @@ func reapServers() error {
|
|||
if pid == currentPID {
|
||||
continue
|
||||
}
|
||||
if !ollamaServeProcess(pid) {
|
||||
slog.Debug("skipping non-server ollama process", "pid", pid)
|
||||
continue
|
||||
}
|
||||
|
||||
proc, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -101,7 +101,34 @@ func terminated(pid int) (bool, error) {
|
|||
return true, nil
|
||||
}
|
||||
|
||||
// reapServers kills all ollama processes except our own
|
||||
func ollamaServeProcess(pid int) bool {
|
||||
cmd := exec.Command("wmic", "process", "where", fmt.Sprintf("ProcessId=%d", pid), "get", "CommandLine", "/value")
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
slog.Debug("failed to inspect ollama process", "pid", pid, "err", err)
|
||||
return false
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(string(output), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
commandLine, ok := strings.CutPrefix(line, "CommandLine=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
fields := strings.Fields(strings.ToLower(commandLine))
|
||||
for i, field := range fields {
|
||||
if strings.Trim(field, `"`) == "serve" && i > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// reapServers kills external ollama serve processes except our own.
|
||||
func reapServers() error {
|
||||
// Get current process ID to avoid killing ourselves
|
||||
currentPID := os.Getpid()
|
||||
|
|
@ -138,6 +165,10 @@ func reapServers() error {
|
|||
if pid == currentPID {
|
||||
continue
|
||||
}
|
||||
if !ollamaServeProcess(pid) {
|
||||
slog.Debug("skipping non-server ollama process", "pid", pid)
|
||||
continue
|
||||
}
|
||||
|
||||
cmd := exec.Command("taskkill", "/F", "/PID", pidStr)
|
||||
if err := cmd.Run(); err != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue