server_unix.go 2.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//go:build darwin

package server

import (
	"context"
	"errors"
	"fmt"
	"log/slog"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"
)

var (
	pidFile       = filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "Ollama", "ollama.pid")
	serverLogPath = filepath.Join(os.Getenv("HOME"), ".ollama", "logs", "server.log")
)

func commandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
	return exec.CommandContext(ctx, name, arg...)
}

func terminate(proc *os.Process) error {
	return proc.Signal(os.Interrupt)
}

func terminated(pid int) (bool, error) {
	proc, err := os.FindProcess(pid)
	if err != nil {
		return false, fmt.Errorf("failed to find process: %v", err)
	}

	err = proc.Signal(syscall.Signal(0))
	if err != nil {
		if errors.Is(err, os.ErrProcessDone) || errors.Is(err, syscall.ESRCH) {
			return true, nil
		}

		return false, fmt.Errorf("error signaling process: %v", err)
	}

	return false, nil
}

// reapServers kills all ollama processes except our own
func reapServers() error {
	// Get our own PID to avoid killing ourselves
	currentPID := os.Getpid()

	// Use pkill to kill ollama processes
	// -x matches the whole command name exactly
	// We'll get the list first, then kill selectively
	cmd := exec.Command("pgrep", "-x", "ollama")
	output, err := cmd.Output()
	if err != nil {
		// No ollama processes found
		slog.Debug("no ollama processes found")
		return nil //nolint:nilerr
	}

	pidsStr := strings.TrimSpace(string(output))
	if pidsStr == "" {
		return nil
	}

	pids := strings.Split(pidsStr, "\n")
	for _, pidStr := range pids {
		pidStr = strings.TrimSpace(pidStr)
		if pidStr == "" {
			continue
		}

		pid, err := strconv.Atoi(pidStr)
		if err != nil {
			slog.Debug("failed to parse PID", "pidStr", pidStr, "err", err)
			continue
		}
		if pid == currentPID {
			continue
		}

		proc, err := os.FindProcess(pid)
		if err != nil {
			slog.Debug("failed to find process", "pid", pid, "err", err)
			continue
		}

		if err := proc.Signal(syscall.SIGTERM); err != nil {
			// Try SIGKILL if SIGTERM fails
			if err := proc.Signal(syscall.SIGKILL); err != nil {
				slog.Warn("failed to stop external ollama process", "pid", pid, "err", err)
				continue
			}
		}

		slog.Info("stopped external ollama process", "pid", pid)
	}

	return nil
}