inanJeong
이난's 프로그래밍 지식 사이트
inanJeong
전체 방문자
오늘
어제
  • 분류 전체보기 (83)
    • 회고록 및 개발일기 (4)
    • Advanced (0)
    • Python (18)
      • 파이썬 기초 (0)
      • 파이썬스럽게 개발하기 (6)
      • Django (5)
      • 고성능 파이썬 (0)
      • matplotlib (3)
      • Error (4)
    • 기타 (61)
      • Shell Script (2)
      • 라즈베리파이 (5)
      • 티스토리 꾸미기 (2)
      • 알고리즘 (2)
      • 소프트웨어 개발론 (0)
      • JIRA(프로젝트 관리 도구) (1)
      • git (1)
      • Swagger (1)
      • docker (2)
      • web_server (2)
      • MySQL (2)
      • front_end (3)
      • javascripts(typescript + ES.. (5)
      • ServerSideApplication (1)
      • Data Engineering (5)
      • flutter (2)
      • JSP (10)
      • Spring boot (5)
      • React (3)
      • elasticSearch (1)
      • AWS (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
inanJeong

이난's 프로그래밍 지식 사이트

[라즈베리파이4, python3] 파이썬 프로세스 이름 변경(python process rename)
기타/라즈베리파이

[라즈베리파이4, python3] 파이썬 프로세스 이름 변경(python process rename)

2021. 2. 9. 18:31

 

 

리눅스에서 파이썬 프로그램을 실행하면 모든 프로세스 이름(CMD:COMMAND)는 python3로 나타나게 됩니다.(2.x는 python)

5446 5444 16799 pts/5 00:00:00 python3
5447 5444 16799 pts/5 00:00:00 python3
5448 5444 16799 pts/5 00:00:00 python3
5449 5444 16799 pts/5 00:00:00 python3
5450 5444 16799 pts/5 00:00:00 python3
5451 5444 16799 pts/5 00:00:00 python3
5452 5444 16799 pts/5 00:00:00 python3
5453 5444 16799 pts/5 00:00:00 python3

다음과 같이 프로세스 아이디가 모두 python3과 같이 고정이 되며 pkill 사용시 동시에 모든 프로세스가 종료되어 버립니다.

 

C언어에서는 간단하게 프로세스의 이름을 바꿀 수 있는데 파이썬에서 프로세스 이름을 바꾸기는 영 쉽지가 않습니다.

 

몇주간 검색끝에 방법을 찾아냈습니다!!! 해당 내용의 원본사이트 주소입니다!

blog.abhi.host/blog/2010/10/18/changing-process-name-of-python-script/

 

def set_procname(newname):
	from ctypes import cdll, byref, create_string_buffer
	libc = cdll.LoadLibrary('libc.so.6')    #Loading a 3rd party library C
	buff = create_string_buffer(len(newname)+1) #Note: One larger than the name (man prctl says that)
	buff.value = newname                 #Null terminated string as it should be
	libc.prctl(15, byref(buff), 0, 0, 0) #Refer to "#define" of "/usr/include/linux/prctl.h" for the misterious value 16 & arg[3..5] are zero as the man page says.

이 소스는 원본 파일 소스이며 그냥 바로 사용시 TypeError가 발생합니다. 따라서 다음과 같이 수정하고 사용하시면 됩니다.

 

def set_procname(newname):
	from ctypes import cdll, byref, create_string_buffer
	libc = cdll.LoadLibrary('libc.so.6')
	buff = create_string_buffer(len(newname)+1)
	byte_newname = bytes(newname, 'utf-8')
	buff.value = byte_newname
	libc.prctl(15, byref(buff), 0, 0, 0)

 

다음 함수를 사용하여 파이썬에 가져다 쓰면 프로세스의 이름을 정상적으로 변경할 수 있습니다.

(Raspberry Pi4, Python 3.7.3에서 동작 확인)

 

이 글을 보시는 분은 쉽고 간단하게 정보를 얻어가셨으면 좋겠네요^^ 감사합니다.

 

'기타 > 라즈베리파이' 카테고리의 다른 글

[RaspberryPi 4] 모니터 없이 부팅  (0) 2021.08.11
[RaspberryPi 4] 부팅시 프로그램 자동시작  (2) 2021.01.20
[RaspberryPi 4] wpa_supplicant를 이용한 와이파이 설정 + 우선순위 변경  (2) 2021.01.20
[RaspberryPi 4] 인터넷 연결 시 특정 파일 실행  (0) 2021.01.20
    '기타/라즈베리파이' 카테고리의 다른 글
    • [RaspberryPi 4] 모니터 없이 부팅
    • [RaspberryPi 4] 부팅시 프로그램 자동시작
    • [RaspberryPi 4] wpa_supplicant를 이용한 와이파이 설정 + 우선순위 변경
    • [RaspberryPi 4] 인터넷 연결 시 특정 파일 실행
    inanJeong
    inanJeong
    저작권 문제시 이메일 발송 부탁드립니다.(해당 게시글 바로 내리겠습니다.) ghjklla007@naver.com

    티스토리툴바