Shell 스크립트의 이해
왜 Shell 스크립트가 필요한가?
Worker 컨테이너가 Shell 스크립트를 수행하는 방법
Worker 컨테이너는 외부의 파일을 사용할 수 없다.
내부에서 파일을 만들면 Worker 컨테이너를 재시작하면 사라진다. (컨테이너가 실행될때 초기화돼서 실행되는 docker의 특징)
해결방법

실행 권한 추가하기
chmod +x select_fruit.sh
select_fruit.sh 파일
FRUIT=$1
if [ $FRUIT = "APPLE" ]; then
echo "You selected apple."
elif [ $FRUIT = "ORANGE" ]; then
echo "You selected orange."
elif [ $FRUIT = "GRAPE" ]; then
echo "You selected grape."
else
echo "You selected other fruit."
fi
실행 방법
./select_fruit.sh APPLE
Bash Operator로 컨테이너 외부의 쉘 스크립트 수행하기
from airflow import DAG
import pendulum
from airflow.operators.bash import BashOperator
with DAG(
dag_id="dags_bash_select_fruit",
schedule="10 0 * * 6#1",
start_date=pendulum.datetime(2023, 3, 1, tz="Asia/Seoul"),
catchup=False
) as dag:
t1_orange = BashOperator(
task_id="t1_orange",
bash_command="/opt/airflow/plugins/shell/select_fruit.sh ORANGE"
)
t2_avocado = BashOperator(
task_id="t1_avocado",
bash_command="/opt/airflow/plugins/shell/select_fruit.sh AVOCADO"
)
t1_orange >> t2_avocado