Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kerus1024/3f7d82a68bc38afc153f94dd8af8ceea to your computer and use it in GitHub Desktop.
Save kerus1024/3f7d82a68bc38afc153f94dd8af8ceea to your computer and use it in GitHub Desktop.
Bash Shellscript run_multiple_process_with_features_restart_process_when_single_process_failed_and_daily_restarts.bash
#!/bin/bash
PID=$$
RESTART_SECONDS_AFTER=15
get_date() {
echo $(TZ="Asia/Seoul" date +%Y%m%d)
}
run_jobs() {
$@ &
JOBS+=($!)
}
JOBS=()
LAST_DATE=$(get_date)
DAILY_RESTART_HOUR=4 # 0 ~ 23
DUMMY_JOBS=
while true;
do
if [ ! -z "${JOBS[@]}" ]; then
echo "${JOBS[@]}" | xargs -r kill -9
echo "--------------------------------------"
echo -e "\n\n\n\n\nProcess (Re)starts\n\n\n\n"
echo "--------------------------------------"
fi
JOBS=()
run_jobs bash -c "exec -a '[bash daily schedule $PID]: dummy' sleep 10000000" &
DUMMY_JOBS=$!
cd /home/kerus1024/datamerge/
run_jobs node app.js
cd /home/kerus1024/crawler
run_jobs node app.js a
run_jobs node app.js b
run_jobs node app.js c
{
while true
do
if [ "$(get_date)" != "$LAST_DATE" ]; then
if [ "$(date +%k)" = "$DAILY_RESTART_HOUR" ]; then
LAST_DATE=$(get_date)
kill -9 $DUMMY_JOBS
echo "!!!!! Process restarts to scheduled !!!!"
break
fi
fi
bash -c "exec -a '[bash daily schedule $PID]:' sleep 10000000"
done
} &
wait -n ${JOBS[@]}
bash -c "exec -a '[bash schedule $PID]:' sleep $RESTART_SECONDS_AFTER"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment