script

import _winreg as wreg
import os
import sys

###### Constants
CurrentKey="Software\Microsoft\Windows\CurrentVersion\Run"
WindowsStartUpFolder="C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp"
PythonPath=sys.executable

def run_script_with_windows_scheduler(script_path):
    command_to_schedule = "schtasks /create /tn \"System-App\" /tr \"" + PythonPath + " " + script_path + "\" /sc onstart"
    os.system(command_to_schedule)

def add_to_startup_folder(script_path):
    with open(WindowsStartUpFolder + '\\' + "open.bat", "w+") as bat_file:
            bat_file.write(r'start "" %s' % PythonPath + " " + script_path)

def add_to_startup_registry_keys(script_path):
    aKey = wreg.OpenKey(wreg.HKEY_CURRENT_USER, CurrentKey, 0, wreg.KEY_ALL_ACCESS)
    wreg.SetValue(aKey, 'system321', wreg.REG_SZ, PythonPath + " " + script_path)
    wreg.CloseKey()

def run_script_as_windows_service(script_path):
    register_service_command = "sc create SystemApp binPath =" + PythonPath + " --" + script_path
    os.system(register_service_command)

def persistent_on_windows(script_path, start_way):
    options = {0: run_script_with_windows_scheduler,
               1: add_to_startup_folder,
               2: add_to_startup_registry_keys,
               3: run_script_as_windows_service}
    options[start_way](script_path)

def create_initd_file(script_path):
    install_file_path=script_path + "\\systemapp"

    with open(install_file_path, 'r') as file:
        filedata = file.read()

    # Replace the target string
    filedata = filedata.replace("python-source",PythonPath)
    filedata = filedata.replace("python-source", script_path)

    # Write the file out again
    with open(install_file_path, 'w') as file:
        file.write(filedata)

    os.system("chkconfig --add systemapp")
    os.system("chkconfig --level 2345" + "systemapp")

    ## Taken from:
    ## https://unix.stackexchange.com/questions/20357/how-can-i-make-a-script-in-etc-init-d-start-at-boot

    run_on_windows("C:\\Logs\\App\\TestApplication.exe", 3)





#!/bin/bash
# Source function library.
. /etc/init.d/functions

start() {
# code to start app comes here
# example: daemon program_name &
}

stop() {`
# code to stop app comes here
# example: killproc program_name
}

case “$1” in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
# code to check status of app comes here
# example: status program_name
;;
*)
echo “Usage: $0 {start|stop|status|restart}”
esac

exit 0

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *