- 1 year ago
Airflow to execute a file on Windows server
Comment
1 / 1
To execute a file on a Windows server using Apache Airflow, you can use the BashOperator
or the PythonOperator
with appropriate commands or scripts.
Here's an example using the BashOperator
:
pythonfrom airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime
# Define the DAG
dag = DAG(
'execute_file',
description='DAG to execute a file on Windows server',
schedule_interval=None,
start_date=datetime(2023, 1, 1),
)
# Define the BashOperator to execute the file
execute_file_task = BashOperator(
task_id='execute_file',
bash_command='cmd /c "path/to/your/file.bat"',
dag=dag
)
# Set the task dependencies
execute_file_task
Replace 'path/to/your/file.bat'
with the actual path to the file you want to execute on the Windows server.
Alternatively, you can use the PythonOperator
if you need more control over the execution logic. Here's an example:
pythonfrom airflow import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime
import subprocess
# Define a function to execute the file
def execute_file():
subprocess.run('path/to/your/file.bat', shell=True)
# Define the DAG
dag = DAG(
'execute_file',
description='DAG to execute a file on Windows server',
schedule_interval=None,
start_date=datetime(2023, 1, 1),
)
# Define the PythonOperator to call the execute_file function
execute_file_task = PythonOperator(
task_id='execute_file',
python_callable=execute_file,
dag=dag
)
# Set the task dependencies
execute_file_task
Again, replace 'path/to/your/file.bat'
with the actual path to the file you want to execute.
Make sure you have Airflow properly set up and configured to run on your Windows server.