
몇 주마다 실행이 중지되는 바이너리 파일이 있습니다. 간단히 다시 시작하면 몇 주 동안 실행됩니다. 소프트웨어가 완벽하지는 않지만 자동으로 다시 시작하면 충분할 것입니다.
바이너리가 실행 중인지 자동으로 확인하고 그렇지 않은 경우 시작하도록 설정하는 가장 좋은 방법은 무엇입니까?
답변1
프로그램을 제어하는 우아한 방법은 PID 파일 메커니즘을 사용하여 스크립트를 작성하여 프로그램의 PID를 파일에 저장하고 프로세스가 아직 실행 중인지 확인하는 것입니다.
#!/bin/bash
PID_FILE=/var/run/rerun.pid # need root permission to store in this directory
EXEC=/usr/bin/gedit # replace it with actual executable
function run() {
# execute the program
$EXEC &
# save its PID
echo $! > $PID_FILE
}
if [ -e $PID_FILE ]; then
# check if program is still running
pid=$(<$PID_FILE)
# find the proper process
ps --pid $pid|grep -q `basename $EXEC`
if [ $? != 0 ]; then
# not found - rerun
run
fi
else
# no PID file - just execute
run
fi
이제 스크립트를 주기적으로 실행하십시오(예:크론탭).