|
1 | | -#!/usr/local/munki/munki-python |
| 1 | +#!/bin/bash |
2 | 2 |
|
3 | | -import os |
4 | | -import subprocess |
5 | | -import sys |
| 3 | +TOUCH_FILE_PATH="/Users/Shared/.com.github.munkireport.run" |
| 4 | +LAUNCHD="com.github.munkireport.runner" |
| 5 | +LAUNCHD_PLIST="/Library/LaunchDaemons/${LAUNCHD}.plist" |
| 6 | +SUBMIT_SCRIPT="/usr/local/munkireport/munkireport-runner" |
6 | 7 |
|
7 | | -TOUCH_FILE_PATH = '/Users/Shared/.com.github.munkireport.run' |
8 | | -LAUNCHD = 'com.github.munkireport.runner' |
9 | | -LAUNCHD_PATH = '/Library/LaunchDaemons/{}.plist'.format(LAUNCHD) |
10 | | -SUBMIT_SCRIPT = '/usr/local/munkireport/munkireport-runner' |
| 8 | +rm -f "$TOUCH_FILE_PATH" |
| 9 | +touch "$TOUCH_FILE_PATH" |
11 | 10 |
|
12 | | -def write_touch_file(): |
13 | | - if os.path.exists(TOUCH_FILE_PATH): |
14 | | - os.remove(TOUCH_FILE_PATH) |
15 | | - |
16 | | - if not os.path.exists(TOUCH_FILE_PATH): |
17 | | - with open(TOUCH_FILE_PATH, 'a'): |
18 | | - os.utime(TOUCH_FILE_PATH, None) |
19 | | - |
20 | | -def ensure_launchd_loaded(): |
21 | | - cmd =[ |
22 | | - '/bin/launchctl', |
23 | | - 'list' |
24 | | - ] |
25 | | - loaded_launchds = subprocess.check_output(cmd).decode('utf-8', 'ignore') |
26 | | - # load the launchd if it's not loaded and is present on disk |
27 | | - if LAUNCHD not in loaded_launchds and os.path.exists(LAUNCHD_PATH): |
28 | | - cmd = [ |
29 | | - '/bin/launchctl', |
30 | | - 'load', |
31 | | - LAUNCHD_PATH |
32 | | - ] |
33 | | - subprocess.check_call(cmd) |
34 | | - |
35 | | -def main(): |
36 | | - write_touch_file() |
37 | | - ensure_launchd_loaded() |
38 | | - # If the launchd isn't present, call the submit script old school |
39 | | - if not os.path.exists(LAUNCHD_PATH): |
40 | | - subprocess.check_call(SUBMIT_SCRIPT) |
41 | | - |
42 | | -if __name__ == '__main__': |
43 | | - main() |
| 11 | +if [[ -f "$LAUNCHD_PLIST" ]]; then |
| 12 | + if ! launchctl list | grep -q "$LAUNCHD"; then |
| 13 | + launchctl bootstrap system "$LAUNCHD_PLIST" 2>/dev/null || \ |
| 14 | + launchctl load -w "$LAUNCHD_PLIST" 2>/dev/null || true |
| 15 | + fi |
| 16 | + if ! launchctl list | grep -q "$LAUNCHD"; then |
| 17 | + [[ -x "$SUBMIT_SCRIPT" ]] && "$SUBMIT_SCRIPT" |
| 18 | + fi |
| 19 | +else |
| 20 | + [[ -x "$SUBMIT_SCRIPT" ]] && "$SUBMIT_SCRIPT" |
| 21 | +fi |
0 commit comments