0Pricing
Pandas & NumPy Academy · 강의

Pipeline 실행 예약과 로깅

명령줄에서 Python 스크립트로 pipeline을 실행하고 시작 및 종료 시간을 기록하며 cron 또는 스케줄러로 자동화합니다.

Pipeline 실행 예약과 로깅은(는) CoddyKit의 무료 Pandas & NumPy Academy 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Pandas & NumPy Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

From Notebook to Script

A pipeline that runs only when a developer manually opens a notebook provides no business value beyond the first run. To run automatically every day, the pipeline must be structured as a Python script that is executable from the command line: python pipeline.py. This requires a if __name__ == '__main__': entry point, command-line argument parsing, and proper logging — the three pillars of a production script.

# pipeline.py
import argparse
import logging
import pandas as pd

def main(config_path):
    logging.info(f'Starting pipeline with config: {config_path}')
    # ... run ETL steps ...
    logging.info('Pipeline complete.')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', default='config.json')
    args = parser.parse_args()
    main(args.config)

Configuring Python Logging

Python's built-in logging module is the correct tool for pipeline logs — not print() statements. Configure a logger with both console output and file output using logging.basicConfig(). Log at the INFO level for normal progress and ERROR for failures. File-based logs persist after the process exits, which is essential for debugging scheduled runs that no one was watching.

import logging
from datetime import date

log_file = f'pipeline_{date.today()}.log'

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s',
    handlers=[
        logging.FileHandler(log_file),
        logging.StreamHandler()
    ]
)

logging.info('Logger configured.')

Logging Pipeline Start and End

Always log the start time, end time, and elapsed time of a pipeline run. This establishes a baseline: if the pipeline normally runs in 45 seconds and today it took 8 minutes, something changed — perhaps the input file is 10× larger or a database query is running slowly. Time-stamped start and end log entries make this comparison trivial from the log file alone.

import time
import logging

def run_pipeline(config):
    start = time.time()
    logging.info(f'Pipeline START | env={config.get("env", "dev")} | input={config["input_path"]}')

    try:
        df = extract(config)
        df_clean = transform(df, config)
        load(df_clean, config)
        elapsed = time.time() - start
        logging.info(f'Pipeline SUCCESS | rows={len(df_clean)} | elapsed={elapsed:.1f}s')
    except Exception as e:
        logging.error(f'Pipeline FAILED | error={e}', exc_info=True)
        raise

Logging Row Counts Per Step

Log the row count entering and exiting each transformation step. A clean log looks like: extract: 50,000 rows → drop_nulls: 49,200 rows → filter: 47,800 rows → output: 47,800 rows. This trace makes it immediately clear how many rows were dropped at each step and whether the numbers are expected. Anomalous drops show up as gaps in the logged counts.

def log_step(df, step_name):
    logging.info(f'{step_name}: {len(df):,} rows')
    return df

import pandas as pd

df = (pd.read_csv('orders.csv')
    .pipe(log_step, 'extract')
    .dropna(subset=['revenue'])
    .pipe(log_step, 'drop_nulls')
    .query('quantity > 0')
    .pipe(log_step, 'filter_qty')
)
print('Step logging complete.')

Scheduling with cron on Linux/Mac

cron is the standard Unix scheduler for recurring jobs. Edit the crontab with crontab -e and add a line specifying when to run the script. The format is: minute hour day month weekday command. A pipeline that must run at 6:00 AM every day uses 0 6 * * * /usr/bin/python /path/to/pipeline.py. Always use absolute paths in cron entries because cron runs in a minimal environment without your shell's PATH settings.

# crontab entry — edit with: crontab -e
# Run pipeline.py at 06:00 every day
# 0 6 * * * /opt/homebrew/bin/python /Users/analyst/pipeline.py --config /Users/analyst/config.json >> /Users/analyst/cron.log 2>&1

# Common cron patterns:
# 0 6 * * *      — daily at 06:00
# 0 */4 * * *    — every 4 hours
# 0 9 * * 1      — every Monday at 09:00
print('Cron schedule format: minute hour day month weekday')

Scheduling with Python schedule Library

The schedule library provides a pure-Python way to run jobs at specified intervals without touching cron. It is useful in environments where cron is not available (Windows) or when you want the scheduler logic inside the Python process itself. Wrap the pipeline in a scheduled job loop and keep the process alive to execute repeatedly.

# pip install schedule
# import schedule, time

# def job():
#     logging.info('Scheduled run starting...')
#     run_pipeline(CONFIG)

# schedule.every().day.at('06:00').do(job)
# schedule.every(4).hours.do(job)

# while True:
#     schedule.run_pending()
#     time.sleep(60)

print('schedule library: use for in-process Python scheduling')

Error Handling and Exit Codes

A pipeline script should return a non-zero exit code when it fails so the scheduler knows the job failed. Wrap the main execution in a try/except block and call sys.exit(1) on failure. cron, Jenkins, and Airflow all check the exit code: a non-zero code triggers an alert, re-run, or notification. An unhandled exception that does not set the exit code may go unnoticed by automated monitoring.

import sys

def main():
    try:
        run_pipeline(CONFIG)
        sys.exit(0)  # success
    except AssertionError as e:
        logging.error(f'Data validation failed: {e}')
        sys.exit(2)  # data error
    except Exception as e:
        logging.error(f'Unexpected error: {e}', exc_info=True)
        sys.exit(1)  # general failure

print('Exit code 0=success, 1=error, 2=data failure')

Writing a Pipeline Run Summary File

After a successful run, write a small JSON summary file alongside the output. Include the run timestamp, input row count, output row count, rows dropped, and elapsed time. Monitoring systems and dashboards can read this file to track pipeline health trends over time. A dashboard showing output rows over the last 30 days makes it easy to spot the day a data source started delivering fewer records.

import json
from datetime import datetime

def write_run_summary(config, input_rows, output_rows, elapsed):
    summary = {
        'run_at': datetime.now().isoformat(),
        'input_path': config['input_path'],
        'input_rows': input_rows,
        'output_rows': output_rows,
        'rows_dropped': input_rows - output_rows,
        'elapsed_seconds': round(elapsed, 2),
        'status': 'success'
    }
    with open('last_run_summary.json', 'w') as f:
        json.dump(summary, f, indent=2)
    print('Run summary written.')

Idempotent Scheduling: Avoiding Double Runs

If a scheduled pipeline is triggered twice accidentally, it should not corrupt the output. Design the load step to be idempotent: use a dated output file name, or overwrite the same output with the latest result. For database loads, use if_exists='replace' or an UPSERT pattern. Never use append mode without a deduplication step, or each scheduled run will add duplicate rows to the output table.

from datetime import date

def load_idempotent(df, config):
    # Date-stamped output: each run overwrites its own day's file
    output_path = f"output_{date.today().strftime('%Y%m%d')}.parquet"
    df.to_parquet(output_path, index=False)
    logging.info(f'Loaded {len(df)} rows to {output_path}')

Alerting on Pipeline Failure

For pipelines that business operations depend on, silence after a failure is dangerous. Set up a simple alert: if the run summary file is not updated within the expected window, send an email or Slack message. Python's smtplib can send an email on failure, or you can use a webhook to post to Slack. Alert immediately on exit code 1 or 2 so the analyst knows the daily refresh missed before the business notices.

import smtplib

def send_failure_alert(error_msg):
    # Example: send plain-text email via SMTP
    # server = smtplib.SMTP('smtp.example.com', 587)
    # server.sendmail('pipeline@company.com',
    #                 'analyst@company.com',
    #                 f'Subject: Pipeline Failed\n\n{error_msg}')
    # server.quit()
    print(f'[ALERT] Would send failure notification: {error_msg}')

# In main():
# except Exception as e:
#     send_failure_alert(str(e))
#     sys.exit(1)
print('Alert integration pattern shown above.')

Complete Scheduled Pipeline Script

Combine all the pieces — argument parsing, logging config, run summary, error handling, and exit codes — into a complete pipeline script. This script can be dropped into any environment, pointed at a config file, and scheduled with cron or any workflow orchestrator. It produces a dated log file, a run summary, and a dated output file with every execution, making every run fully auditable and independently reproducible.

# Full script skeleton:
# 1. parse --config argument
# 2. configure logging to file + console
# 3. load JSON config
# 4. validate config
# 5. run extract() -> transform() -> load()
# 6. write run summary JSON
# 7. sys.exit(0) on success, sys.exit(1) on failure

print('Production pipeline script structure complete.')
print('Schedule with: crontab -e  or  python scheduler.py')

Quick Check

Test your understanding of Data Analysis concepts from this lesson.

Lesson Recap

In this lesson you learned: structuring a pipeline as a command-line script with argument parsing and logging, scheduling with cron and handling failures with non-zero exit codes and alerts, and writing run summary files and designing idempotent load steps for reliable automated execution. Congratulations on completing the Data Analysis: Pandas and NumPy track!

자주 묻는 질문

“Pipeline 실행 예약과 로깅” 강의는 무료인가요?

네 — “Pipeline 실행 예약과 로깅” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Pandas & NumPy Academy 강의 전체를 잠금 해제할 수 있습니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“Pipeline 실행 예약과 로깅”에서 뭘 배우나요?

명령줄에서 Python 스크립트로 pipeline을 실행하고 시작 및 종료 시간을 기록하며 cron 또는 스케줄러로 자동화합니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Pandas & NumPy Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Pandas & NumPy Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“Pipeline 실행 예약과 로깅” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Pandas & NumPy Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Pandas & NumPy Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 변환 단계를 함수로 구성하기
  2. 설정 딕셔너리로 Pipeline 매개변수화하기
  3. 단언으로 Pipeline 단계 test하기
  4. Pipeline 실행 예약과 로깅
← Pandas & NumPy Academy(으)로 돌아가기