postfix가 실행 중인지 테스트

postfix가 실행 중인지 테스트

postfixbash 스크립트 내부에서 무언가가 실행되고 있는지 테스트할 수 있는 안정적인 방법을 찾고 있습니다 .

나의 첫 번째 시도는 단지 시도에 불과했고 pidof postfix작동하지 않았습니다.

그런 다음 다음을 얻으려고합니다 postfix status.

POSTFIX_LOCATION=/var/packages/MailServer/target/sbin/postfix # location of postfix
result=`$POSTFIX_LOCATION status`
if [ -z $result ]; then
    echo "Error: No status output from postfix"
elif [[ "$result" == *"is running"* ]]; then
    echo "postfix is running!"
else echo "postfix is not running!"
fi

그러나 상태가 콘솔에 보고되더라도 result변수는 여전히 비어 있습니다. 콘솔 출력은 다음과 같습니다.

postfix/postfix-script: the Postfix mail system is running: PID: 11996
Error: No status output from postfix

PID: 11996마침내 프로세스 이름("마스터")을 가져와서 postfix가 실행 중인지 테스트하는 방법을 찾았습니다. 그래서 다음하다일하다:

pidof master

postfix그러나 이는 매우 상세하지 않으며 이것이 실행 중인지 테스트하는 신뢰할 수 있는 방법 인지 확실하지 않습니다 .

그래서 내 질문은 다음과 같습니다

  • postfix statusBash 스크립트 내부에서 출력을 얻는 방법은 무엇입니까? - 내가 잘못한 게 있나요?
  • 더 좋은 게 있나요?믿을 수 있는postfixbash 스크립트 내부에서 실행되는지 테스트하는 방법은 무엇입니까?

답변1

데비안 또는 우분투

sudo service postfix status

답변2

postfix가 실행 중인지 테스트하는 신뢰할 수 있는 방법:

if /var/packages/MailServer/target/sbin/postfix status; then
    echo "postfix is running!"
else echo "postfix is not running!"
fi

본질적으로 동일한 기능을 갖춘 대안:

if /var/packages/MailServer/target/libexec/master -t 2>/dev/null; then
    echo "postfix is not running!"
else echo "postfix is running!"
fi

추리

ps | grep [p]ostfix더 많은 조사를 한 후에 postfix가 실행 중인지 확인하기 위한 테스트가 전혀 신뢰할 수 없다는 것을 발견했습니다 .

/var/packages/MailServer/target/sbin/postfix status유용한 것을 출력하지 않는 것 같은 이유는 postfix내부 postlog바이너리가 출력에 사용되기 때문입니다. 관련 코드는 bash 스크립트에서 찾을 수 있습니다 /var/packages/MailServer/target/libexec/postfix-script.

LOGGER="$command_directory/postlog -t $MAIL_LOGTAG/postfix-script"
INFO="$LOGGER -p info"

조사하다종료 코드postfix status반면에 정말 잘 작동합니다. 실제로는 postfix-script바이너리에 의해 뒤에서 호출되며 postfix직접 호출할 수 없습니다. 그러나 다행히 스크립트에는 postfix실행 중인지 테스트하는 코드도 포함되어 있습니다.

status)
    $daemon_directory/master -t 2>/dev/null && {
        $INFO the Postfix mail system is not running
        exit 1
    }
    $INFO the Postfix mail system is running: PID: `sed 1q pid/master.pid`
    exit 0
    ;;

이 변수는 $daemon_directory호출하는 바이너리에 의해 설정됩니다 postfix. 내 시스템에서는 /var/packages/MailServer/target/libexec/.

답변3

Postfix가 실행 중인지 확인하세요.

sudo /etc/init.d/postfix status

시작 접미사:

sudo /etc/init.d/postfix start

접미사를 중지하려면 다음을 수행하십시오.

sudo /etc/init.d/postfix stop

답변4

나는 매우 적은 수의 일반 명령이 설치된 매우 간단한 도커 컨테이너 내에서 postfix를 실행하고 있습니다. 하지만 netstat이 있습니다. 따라서 netstat를 사용하여 프로세스가 수신 중인 포트를 나열하십시오. smtp 포트(예: postfix)에서 수신 대기 중인 프로세스를 찾습니다.

if ! netstat -l | grep smtp; then
    echo "postfix is not running!"
else echo "postfix is running!"
fi

관련 정보