简体   繁体   中英

How to export Scheduled Task to other machines

I build an app in python and inside that app i am scheduling a task which is executing another exe file and i am scheduling it through xml file. Now i want to deploy my app on other systems for this i am sending that exe with my app lets suppose i copying it in appdata folder of clients pc now my issue how our xml file execute that exe on client's pc. below is my xml file.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2022-01-05T10:35:29</Date>
    <URI>\SecTEL</URI>
  </RegistrationInfo>
  <Triggers>
    <TimeTrigger>
      <Repetition>
        <Interval>PT20M</Interval>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2022-01-05T10:35:00</StartBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
    <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <Duration>PT10M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>false</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\wamp64\www\project\python\sectel.exe</Command>
    </Exec>
  </Actions>
</Task>

This is my python code for scheduling task through xml file firstly i am copying my exe file on target's pc after that i am scheduling a task.

    def resource_path(relative_path):
        """ Get absolute path to resource, works for dev and for PyInstaller """
        try:
            # PyInstaller creates a temp folder and stores path in _MEIPASS,
            # and places our data files in a folder relative to that temp
            # folder named as specified in the datas tuple in the spec file
            base_path = os.path.join(sys._MEIPASS, 'data')
        except Exception:
            # sys._MEIPASS is not defined, so use the original path
            base_path = 'C:\\wamp64\\www\\project\\python'
    
        return os.path.join(base_path, relative_path)
    
    path = os.getenv('APPDATA')
        file = resource_path ( "sectel.exe" )
        shutil.copy ( file , path )
    p= (datetime.datetime.now()+datetime.timedelta(minutes2)).strftime('%X')
    
    
        cmd = "schtasks /create /f /XML "+ file+" /tn " + "SecTEL"
    
        deviceInfo = subprocess_check_output ( cmd )

<Actions Context="Author">
    <Exec>
      <Command>C:\wamp64\www\project\python\sectel.exe</Command>
    </Exec>
  </Actions>

when i send my app on some other pc and execute the exe file then app runs fine but the other exe which i schedule through my makes an entry in task scheduler but never run and gives this error the system cannot find the file specified i know the issue is in my xml file code because the path i am giving here inside command is not valid for other pc it is only valid for my own machine.

So when you execute the exe file it runs fine (on the other machine), but when you schedule through your python script, it makes an entry in task scheduler but never runs and gives the error the system cannot find the file specified .

I think it is likely that one of the following is happening:

  1. sys._MEIPASS is defined in the other OS environment variables, so it uses a different base_path than default 'C:\wamp64\www\project\python'. But then you do <Command>C:\wamp64\www\project\python\sectel.exe</Command> which is forcing to run on that folder.

If this is the case, and your target machine is always the same, you can modify your xml to whatever the actual path is (try printing the value of file after file = resource_path ( "sectel.exe" ) ) and see what the path looks like, or create your XML file programmatically using something like ElementTree . This second approach will work on any machine.

  1. On another possible scenario, path = os.getenv('APPDATA') is used to then copy from file (source) to path (destination) with shutil.copy ( file, path ) . So in case you APPDATA is different from the path in your xml C:\wamp64\www\project\python\sectel.exe , then it won't be able to find the file, as it got copied somewhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM