Skip to content

Instantly share code, notes, and snippets.

@deptno
Last active December 9, 2024 16:56
Show Gist options
  • Save deptno/63426d27ad563c97e319901952720d57 to your computer and use it in GitHub Desktop.
Save deptno/63426d27ad563c97e319901952720d57 to your computer and use it in GitHub Desktop.
simple task runner queue for shell
SCRIPT=${1:="model"}
RUN_NAME=${2:-$1}
jupyter nbconvert --to script src/model/model.ipynb --output $SCRIPT
echo "cd src/model; time python $SCRIPT.py $RUN_NAME; date; cd -" >> queue
echo "enqueued"
while true; do
echo "Working"
sleep 1
done
#!/bin/bash
QUEUE_FILE="./queue"
LOG_DIR="./logs"
mkdir -p $LOG_DIR
echo "Watching the queue: $QUEUE_FILE"
CURRENT_PID=0 # 현재 실행 중인 프로세스 ID를 저장
# SIGINT(Ctrl+C) 처리
trap 'echo "Interrupt received. Stopping current job..."; kill $CURRENT_PID 2>/dev/null; CURRENT_PID=0' SIGINT
while true; do
if [ -s "$QUEUE_FILE" ]; then
JOB=$(head -n 1 "$QUEUE_FILE") # 첫 번째 작업 읽기
sed -i '1d' "$QUEUE_FILE" # 큐에서 제거
# 로그 파일 이름 생성 (작업 시간 기반)
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
LOG_FILE="$LOG_DIR/job_${TIMESTAMP}.log"
echo "Running job: $JOB >> $LOG_FILE"
START_TIME=$(date +%s) # 시작 시간 기록
# 작업 실행 (백그라운드 실행 후 PID 저장)
# bash -c "$JOB" > >(sed 's/\^M/\r/g' | tee -a "$LOG_FILE" >"$LOG_DIR/latest") 2>&1 &
bash -c "$JOB" > >(tee -a "$LOG_FILE" >"$LOG_DIR/latest") 2>&1 &
CURRENT_PID=$!
# 작업이 완료될 때까지 대기
wait $CURRENT_PID
CURRENT_PID=0 # 작업 종료 후 초기화
END_TIME=$(date +%s) # 종료 시간 기록
ELAPSED_SECONDS=$((END_TIME - START_TIME))
ELAPSED_MINUTES=$((ELAPSED_SECONDS / 60)) # 초를 60으로 나눔
echo "Finished job: $JOB, $(date), $ELAPSED_MINUTES minutes and $((ELAPSED_SECONDS % 60)) seconds"
fi
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment