시스템 장치의 환경 변수를 표시하는 방법은 무엇입니까?

시스템 장치의 환경 변수를 표시하는 방법은 무엇입니까?

envsystemd 서비스 장치에서 덤프하려고 합니다.systemctl show-environment내가하고 싶은 일을하지 않습니다. systemctl내 서비스 내부의 환경이 어떤지 보여줄 수 있는 방법이 있나요 ?

답변1

서비스가 실행 중인 경우 를 사용 systemctl status <name>.service하여 서비스 프로세스의 PID를 식별한 후 를 사용 sudo strings /proc/<PID>/environ하여 프로세스의 실제 환경을 확인할 수 있습니다.

답변2

"set"을 실행하는 스크립트를 실행하고 출력을 파일에 씁니다.

#!/usr/bin/ksh

set >> /tmp/set-results.txt

답변3

@telcoM의 답변을 자세히 설명하기 위해 다음 코드 한 줄을 사용하여 현재 실행 중인 단일 PID 서비스에서 환경 변수를 덤프할 수 있습니다(일부 조정을 통해 모든 PID를 반복하고 결과를 연결할 수 있음).

strings /proc/$(systemctl status <unitname>.service | grep -Po '(?<=PID: )\d+')/environ

문제의 서비스를 시작하거나 액세스할 수 없는 경우 systemctl명령을 사용하여 서비스를 구성하는 데 사용되는 디스크의 모든 파일 조합을 볼 수 있습니다. 매뉴얼 페이지에서:

cat PATTERN...
           Show backing files of one or more units. Prints the "fragment" and
           "drop-ins" (source files) of units. Each file is preceded by a comment
           which includes the file name. Note that this shows the contents of the
           backing files on disk, which may not match the system manager's
           understanding of these units if any unit files were updated on disk and
           the daemon-reload command wasn't issued since.

따라서 systemctl cat <unitname>.service계산된 구성 "파일"을 보여주는 출력이 생성되어야 합니다. 디스크의 업데이트된 파일에 대한 위의 경고 외에도 지침이 있는 경우 EnvironmentFile전체 그림을 얻으려면 해당 파일을 찾아야 할 수도 있습니다. 이와 같은 내용을 사용하면 매우 가까워질 수 있지만 입력하기는 어려울 수 있습니다.

echo $(systemctl show-environment) $(systemctl cat <unitname>.service | grep -Po '(?<=^Environment=)[^\n]*') $(cat "$(systemctl cat <unitname>.service | grep -Po '(?<=^EnvironmentFile=-)[^\n]*')")

우리가 하는 일은 다음과 같습니다:

  • systemctl show-environment-- systemctl의 현재 환경을 가져옵니다.
  • systemctl cat <unitname>.service | grep -Po '(?<=^Environment=)[^\n]*'-- 관련 부서로부터 환경 지침을 얻습니다.
  • cat "$(systemctl cat <unitname>.service | grep -Po '(?<=^EnvironmentFile=-)[^\n]*')"-- 나열된 파일의 내용을 가져옵니다. 대시가 앞에 붙지 않은 경우 grep 패턴을 약간 조정해야 할 수도 있습니다.
  • echo ...-- 위의 모든 출력을 단일 문자열로 혼합합니다. 이를 수행하는 더 좋은 방법이 있어야합니다.

관련 정보