-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_installer.py
82 lines (73 loc) · 2.3 KB
/
service_installer.py
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
import os
import sys
import platform
def create_systemd_service():
service_content = """[Unit]
Description=File Watcher Service
After=network.target
[Service]
Type=simple
User={user}
ExecStart={python} {script_path} --daemon
Restart=always
[Install]
WantedBy=multi-user.target
""".format(
user=os.getenv('USER'),
python=sys.executable,
script_path=os.path.abspath('app.py')
)
service_path = '/etc/systemd/system/file_watcher.service'
try:
with open(service_path, 'w') as f:
f.write(service_content)
os.system('sudo systemctl daemon-reload')
os.system('sudo systemctl enable file_watcher')
os.system('sudo systemctl start file_watcher')
print("Service installed successfully!")
except Exception as e:
print(f"Error installing service: {str(e)}")
def create_launchd_service():
plist_content = """<?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>com.user.file_watcher</string>
<key>ProgramArguments</key>
<array>
<string>{python}</string>
<string>{script_path}</string>
<string>--daemon</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
""".format(
python=sys.executable,
script_path=os.path.abspath('app.py')
)
plist_path = os.path.expanduser('~/Library/LaunchAgents/com.user.file_watcher.plist')
try:
with open(plist_path, 'w') as f:
f.write(plist_content)
os.system(f'launchctl load {plist_path}')
print("Service installed successfully!")
except Exception as e:
print(f"Error installing service: {str(e)}")
def main():
system = platform.system()
if system == 'Linux':
create_systemd_service()
elif system == 'Darwin': # macOS
create_launchd_service()
elif system == 'Windows':
print("For Windows, please install NSSM (Non-Sucking Service Manager) and run:")
print(f"nssm install FileWatcher {sys.executable} {os.path.abspath('app.py')} --daemon")
else:
print(f"Unsupported operating system: {system}")
if __name__ == "__main__":
main()