Python subprocess.call on sfxcl.exe not working from Windows 2003 Task Scheduler
I have written a script in Python to call SecureFX's commandline tool (sfxcl.exe)
result = subprocess.call([securefx, '/NoPrompt', '/Q', '/RetryCount', retries,
'/RetryDelay', '1', '/Log', sfxLogFile, '/List', '/S', session])
and then converted it into .exe using py2exe.
I can schedule it locally on my WinXP machine and everything works OK. When I run it manually on the Win2003 environment, it also works. BUT when I schedule it using Windows Task Scheduler in Win2003, it runs through the script and doesn't actually call sfxcl.exe (no logs whatsoever). I'm pretty stumped as to why this is...
The Win2003 server does not have Python installed, while my WinXP does (where I developed it).
I'm using Python 2.7.1.
EDIT: Or otherwise, if there is no answer to this - should I consider running the script once and set it to re-run using intervals and what not? Other options? It needs to run every 15-20minutes.
Answer
The issue you're encountering with the Windows Task Scheduler not executing your script properly on the Win2003 server, even though it works locally on your WinXP machine, could be related to several things. Here's a step-by-step approach to diagnose and resolve the issue:
Potential Causes:
-
Environment Differences:
- No Python Installed: As you mentioned, Python is not installed on the Win2003 server, but your script depends on the
py2exe
-compiled.exe
that should run independently. Even though you created an.exe
, the environment (such as PATH variables, permissions, or user context) might be different under the Task Scheduler.
- No Python Installed: As you mentioned, Python is not installed on the Win2003 server, but your script depends on the
-
Task Scheduler Permissions:
- The task might be running under a different user or system account, which could affect permissions for running external programs or accessing files. This is especially critical if
securefx
or the script needs specific permissions (e.g., access to files or network resources).
- The task might be running under a different user or system account, which could affect permissions for running external programs or accessing files. This is especially critical if
-
Working Directory:
- The Task Scheduler might not be setting the correct working directory, which can affect how paths to files or executables are resolved. If the
sfxcl.exe
command relies on relative paths or if your script expects to run in a specific directory, the Task Scheduler may fail to locate the required files.
- The Task Scheduler might not be setting the correct working directory, which can affect how paths to files or executables are resolved. If the
-
Invisible Execution Context:
- Since the Task Scheduler runs tasks without a user interface by default, it could be that the
sfxcl.exe
command is not able to display output (such as logs or error messages) if it's being run in a non-interactive session. This could be why you're seeing no logs.
- Since the Task Scheduler runs tasks without a user interface by default, it could be that the
Debugging Steps:
-
Check Task Scheduler Configuration:
- User Account: Ensure that the Task Scheduler is running the task under a user account that has the necessary permissions to run the script and execute
sfxcl.exe
. Try setting the task to run as the user who manually runs the script successfully. - Run with Highest Privileges: Under the Task Scheduler settings, check the option to "Run with highest privileges" to avoid any permission issues.
- Working Directory: Set the working directory explicitly in the Task Scheduler (or in the script) to ensure
sfxcl.exe
and other files are found. You can specify this under the "Start in" field when configuring the task.
- User Account: Ensure that the Task Scheduler is running the task under a user account that has the necessary permissions to run the script and execute
-
Logging for Debugging:
-
Modify your script to include more robust logging. For example, log the environment variables and current working directory at the beginning of the script to see if there are any discrepancies:
import os import subprocess with open('task_log.txt', 'w') as log_file: log_file.write("Environment Variables:\n") for key, value in os.environ.items(): log_file.write(f"{key}: {value}\n") log_file.write("\nWorking Directory:\n") log_file.write(os.getcwd()) # Now run the securefx command as usual result = subprocess.call([securefx, '/NoPrompt', '/Q', '/RetryCount', retries, '/RetryDelay', '1', '/Log', sfxLogFile, '/List', '/S', session])
-
This will allow you to capture any differences when running under Task Scheduler compared to running manually.
-
-
Use Absolute Paths:
- Ensure that all file paths in your script (e.g., for
sfxcl.exe
, log files, etc.) are absolute paths, as relative paths may not resolve correctly when the script is executed via Task Scheduler.
Example:
securefx = r"C:\Path\To\sfxcl.exe" sfxLogFile = r"C:\Path\To\sfx_log.txt"
- Ensure that all file paths in your script (e.g., for
-
Error Handling in
subprocess.call
:subprocess.call
returns the exit code of the command. You can capture this return value and log it for debugging. This will help to see if there are any errors occurring when running the command:
result = subprocess.call([securefx, '/NoPrompt', '/Q', '/RetryCount', retries, '/RetryDelay', '1', '/Log', sfxLogFile, '/List', '/S', session]) with open('task_log.txt', 'a') as log_file: log_file.write(f"subprocess call returned: {result}\n")
-
Consider Running the Script at Intervals:
- If your script needs to run every 15-20 minutes, consider having it run continuously in a loop with a
time.sleep()
interval. This could be useful if there are potential issues with Task Scheduler or if you need more control over the execution:
import time while True: result = subprocess.call([securefx, '/NoPrompt', '/Q', '/RetryCount', retries, '/RetryDelay', '1', '/Log', sfxLogFile, '/List', '/S', session]) with open('task_log.txt', 'a') as log_file: log_file.write(f"Task completed with result: {result}\n") time.sleep(900) # Sleep for 15 minutes
- If your script needs to run every 15-20 minutes, consider having it run continuously in a loop with a
-
Check Windows Event Logs:
- If there’s an issue with permissions or execution failure, check the Windows Event Viewer for any related logs. This can provide additional details on why the task might not be executing properly.
Conclusion:
Start by adding more logging and checking Task Scheduler permissions. Make sure all paths are absolute, and verify that the task is running under the correct user account with the necessary privileges. Additionally, ensure the working directory is set correctly for the task. If none of this works, consider running the script in a loop with time.sleep()
to control the execution interval manually.