-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_hello_with_core_daemon.sh
More file actions
executable file
·59 lines (43 loc) · 1.74 KB
/
Copy pathrun_hello_with_core_daemon.sh
File metadata and controls
executable file
·59 lines (43 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# This script runs the hello program as a daemon,
# restarting it every time it exits.
#
# Before using this script, you must configure system-wide core dump settings
# using the setup_core_dump_systemwide.sh script.
# If not configured, coredump files will not be created.
# Strict error handling in bash script
set -euo pipefail
# Set working directory, executable path, and log file path
# WORKDIR="/home/jaytwo/workspace/coredump-workspace" # If you register this script as a service, be sure to set an absolute path.
WORKDIR="$(pwd)"
EXEC="${WORKDIR}/hello"
LOG="${WORKDIR}/hello_daemon.log"
cd "${WORKDIR}"
# Allow core dumps (applies to hello run by this script)
ulimit -c unlimited
# ASAN/UBSAN settings (if needed)
export ASAN_OPTIONS="abort_on_error=1:disable_coredump=0"
export UBSAN_OPTIONS="halt_on_error=1:abort_on_error=1"
# Log daemon start
echo "[$(date '+%F %T')] daemon start" >> "${LOG}"
# Limit log file size: if over 10MB, keep only the last 10,000 lines
MAX_LOG_SIZE=10485760 # 10MB
MAX_LOG_LINES=10000
if [ -f "${LOG}" ] && [ $(stat -c%s "${LOG}") -ge $MAX_LOG_SIZE ]; then
tail -n $MAX_LOG_LINES "${LOG}" > "${LOG}.tmp" && mv "${LOG}.tmp" "${LOG}"
echo "[$(date '+%F %T')] log trimmed to last $MAX_LOG_LINES lines" >> "${LOG}"
fi
while true; do
# Log start of execution
echo "[$(date '+%F %T')] starting hello" >> "${LOG}"
# Run
"${EXEC}"
rc=$?
# Log exit
echo "[$(date '+%F %T')] hello exited rc=${rc}" >> "${LOG}"
# Prevent too rapid restarts
sleep 600 # seconds
# NOTE: crash에 의한 소프트웨어 정지가 원인일 확률이 높으며,
# 그럴 경우 재시작하여도 동일한 문제가 반복될 수 있습니다.
# 이 점 유의하시기 바랍니다.
done