-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_service.py
More file actions
129 lines (107 loc) · 3.66 KB
/
install_service.py
File metadata and controls
129 lines (107 loc) · 3.66 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
"""
安装后台检查点服务
支持 systemd (Linux) 和 launchd (macOS)
"""
import os
import sys
import platform
from pathlib import Path
def create_systemd_service(project_root: str, python_path: str):
"""创建 systemd 服务文件 (Linux)"""
service_content = f"""[Unit]
Description=Checkpoint Background Watcher for {Path(project_root).name}
After=multi-user.target
[Service]
Type=simple
User={os.getenv('USER')}
WorkingDirectory={project_root}
ExecStart={python_path} {project_root}/background_watcher.py --project-root {project_root}
Restart=always
RestartSec=10
Environment=PYTHONPATH={project_root}
[Install]
WantedBy=multi-user.target
"""
service_name = f"checkpoint-watcher-{Path(project_root).name.replace('_', '-')}"
service_file = f"/etc/systemd/system/{service_name}.service"
print(f"创建 systemd 服务文件:")
print(f"sudo tee {service_file} << 'EOF'")
print(service_content)
print("EOF")
print()
print("启动服务:")
print(f"sudo systemctl enable {service_name}")
print(f"sudo systemctl start {service_name}")
print()
print("查看状态:")
print(f"sudo systemctl status {service_name}")
print(f"sudo journalctl -u {service_name} -f")
def create_launchd_service(project_root: str, python_path: str):
"""创建 launchd 服务文件 (macOS)"""
user_home = os.path.expanduser("~")
service_name = f"com.checkpoint.watcher.{Path(project_root).name.replace('_', '-')}"
plist_content = f"""<?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>{service_name}</string>
<key>ProgramArguments</key>
<array>
<string>{python_path}</string>
<string>{project_root}/background_watcher.py</string>
<string>--project-root</string>
<string>{project_root}</string>
</array>
<key>WorkingDirectory</key>
<string>{project_root}</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardErrorPath</key>
<string>{project_root}/.checkpoints/watcher.err.log</string>
<key>StandardOutPath</key>
<string>{project_root}/.checkpoints/watcher.out.log</string>
<key>EnvironmentVariables</key>
<dict>
<key>PYTHONPATH</key>
<string>{project_root}</string>
</dict>
</dict>
</plist>
"""
plist_file = f"{user_home}/Library/LaunchAgents/{service_name}.plist"
print(f"创建 launchd 服务文件:")
print(f"cat > {plist_file} << 'EOF'")
print(plist_content)
print("EOF")
print()
print("加载并启动服务:")
print(f"launchctl load {plist_file}")
print(f"launchctl start {service_name}")
print()
print("查看状态:")
print(f"launchctl list | grep {service_name}")
def main():
project_root = os.getcwd()
python_path = sys.executable
print(f"🔧 检查点后台服务安装器")
print(f"📂 项目目录: {project_root}")
print(f"🐍 Python路径: {python_path}")
print()
system = platform.system()
if system == "Linux":
print("检测到 Linux 系统,创建 systemd 服务:")
create_systemd_service(project_root, python_path)
elif system == "Darwin":
print("检测到 macOS 系统,创建 launchd 服务:")
create_launchd_service(project_root, python_path)
else:
print(f"❌ 不支持的系统: {system}")
print("请手动运行后台服务:")
print(f"python {project_root}/background_watcher.py --daemon")
if __name__ == "__main__":
main()