-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_write_task_xml.py
More file actions
70 lines (62 loc) · 2.35 KB
/
_write_task_xml.py
File metadata and controls
70 lines (62 loc) · 2.35 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
"""
Helper called by setup-service.bat.
Writes start-silent.vbs (with full python.exe path) and the Task Scheduler XML.
Usage: python _write_task_xml.py <pyexe> <app_dir> <whoami> <output_xml>
"""
import sys
import os
pyexe = sys.argv[1]
appdir = sys.argv[2]
whoami = sys.argv[3]
outfile = sys.argv[4]
vbs_path = os.path.join(appdir, "start-silent.vbs")
# Write start-silent.vbs with the full python.exe path baked in.
# WScript.Shell.Run with window style 0 hides the console window.
# Using the full path ensures the correct Python/packages are used
# regardless of what PATH looks like in the Task Scheduler environment.
vbs = (
"' Auto-generated by setup-service.bat — do not edit manually.\n"
"' Re-run setup-service.bat to regenerate with correct paths.\n"
"Dim sDir, sCmd\n"
"sDir = CreateObject(\"Scripting.FileSystemObject\").GetParentFolderName(WScript.ScriptFullName)\n"
"sCmd = Chr(34) & \"{pyexe}\" & Chr(34) & \" \" & Chr(34) & sDir & \"\\main.py\" & Chr(34)\n"
"CreateObject(\"WScript.Shell\").Run sCmd, 0, False\n"
).format(pyexe=pyexe)
with open(vbs_path, 'w', encoding='utf-8') as f:
f.write(vbs)
print(" start-silent.vbs written OK")
# Write the Task Scheduler XML — launches wscript.exe with the VBS.
xml = """\
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Description>Objectif.AI - BlueIris AI Detection Server</Description>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
<UserId>{whoami}</UserId>
</LogonTrigger>
</Triggers>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<RestartOnFailure>
<Interval>PT1M</Interval>
<Count>10</Count>
</RestartOnFailure>
<Hidden>true</Hidden>
</Settings>
<Actions Context="Author">
<Exec>
<Command>wscript.exe</Command>
<Arguments>"{vbs_path}"</Arguments>
<WorkingDirectory>{appdir}</WorkingDirectory>
</Exec>
</Actions>
</Task>""".format(vbs_path=vbs_path, appdir=appdir, whoami=whoami)
with open(outfile, 'w', encoding='utf-16') as f:
f.write(xml)
print(" Task XML written OK")