다음과 같은 스크립트가 있습니다.
백그라운드에서 임의의 명령을 실행
터미널 창으로 죽지 않기
그들의 출력을 억제
종료 상태 처리
나는 주로 사용 gedit
, evince
, inkscape
모든 성가신 터미널 출력을 많이 가질 수 등. 명령이 이전 TIMEOUT
에 완료 되면 nohup의 종료 상태가 0 대신 반환됩니다.
#!/bin/bash
TIMEOUT=0.1
#use nohup to run the command, suppressing its output and allowing the terminal to be closed
#also send nohup's output to /dev/null, supressing nohup.out
#run nohup in the background so this script doesn't block
nohup "${@}" >/dev/null 2>&1 &
NOHUP_PID=$!
#kill this script after a short time, exiting with success status - command is still running
#this is needed as there is no timeout argument for `wait` below
MY_PID=$$
trap "exit 0" SIGINT SIGTERM
sleep $TIMEOUT && kill $MY_PID 2>/dev/null & #ignore "No such process" error if this exits normally
#if the command finishes before the above timeout, everything may be just fine or there could have been an error
wait $NOHUP_PID
NOHUP_STATUS=$?
#print an error if there was any. most commonly, there was a typo in the command
[ $NOHUP_STATUS != 0 ] && echo "Error ${@}"
#return the exit status of nohup, whatever it was
exit $NOHUP_STATUS
예 ...
>>> run true && echo success || echo fail
success
>>> run false && echo success || echo fail
Error false
fail
>>> run sleep 1000 && echo success || echo fail
success
>>> run notfound && echo success || echo fail
Error notfound
fail