Ansible – 원격 호스트에서 실행 중인 Linux 프로세스를 Grep(ps -few)하고 종료하는 방법은 무엇입니까?
게시 됨: 2022-02-24Ansible은 매우 놀라운 시스템 관리 도구입니다. 우리는 지난 몇 주 동안 원격 호스트에서 파일을 복사하는 방법, 원격 호스트에서 명령을 실행하는 방법, Java, 원격 호스트에 Python을 설치하는 방법 등에 대해 Ansible에 여러 기사를 게시했습니다.
이 자습서에서는 원격 호스트에서 실행 중인 Java 프로세스를 grep하고 간단한 가능한 플레이북을 사용하여 해당 원격 프로세스를 종료하는 방법을 살펴봅니다.
이 자습서에서 수행할 단계는 다음과 같습니다.
- 원격 호스트에서 CrunchifyAlwaysRunningProgram.java를 실행합니다.
- Java에서 프로그램을 영원히 실행하는 방법에 대한 자습서를 따르십시오.
-
nohup java CrunchifyAlwaysRunningProgram &
1 2 3 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ nohup java CrunchifyAlwaysRunningProgram & [ 1 ] 18174 ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ nohup : ignoring input and appending output to 'nohup.out' |
프로세스가 시작되어 원격 호스트에서 실행 중인지 확인하는 방법은 무엇입니까?
프로세스 ID 18174
를 확인하십시오.
1 2 3 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ ps - few | grep CrunchifyAlwaysRunningProgram ubuntu 18174 15069 1 15 : 15 pts / 0 00 : 00 : 00 java CrunchifyAlwaysRunningProgram ubuntu 18187 15069 0 15 : 16 pts / 0 00 : 00 : 00 grep -- color = auto CrunchifyAlwaysRunningProgram |
- 원격 호스트 IP가 있는 파일
crunchify-hosts
파일을 만듭니다. - grep 및 kill Java 프로세스에 대한 가능한 작업이 있는
crunchify-grep-kill-process.yml
파일 생성 - 실행 명령: ansible-playbook -i ./crunchify-hosts crunchify-grep-kill-process.yml
- macOS 터미널 콘솔에서 결과 확인
crunchify 호스트 파일
1 2 3 4 5 6 7 8 9 10 |
[ local ] localhost ansible_connection = local ansible_python_interpreter = python [ crunchify ] 3.16.83.84 [ crunchify : vars ] ansible_ssh_user = ubuntu ansible_ssh_private_key_file =/ Users / crunchify / Documents / ansible / crunchify . pem ansible_python_interpreter =/ usr / bin / python3 |
파일에는 암호 없이 로그인하는 데 도움이 되는 원격 IP 주소와 자격 증명이 포함되어 있습니다.
crunchify-grep-kill-process.yml 파일
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
--- - hosts : crunchify become : yes tasks : - name : Get running processes list from remote host ignore_errors : yes shell : "ps -few | grep CrunchifyAlwaysRunningProgram | awk '{print $2}'" register : running_processes - name : Kill running processes ignore_errors : yes shell : "kill {{ item }}" with_items : "{{ running_processes.stdout_lines }}" - wait_for : path : "/proc/{{ item }}/status" state : absent with_items : "{{ running_processes.stdout_lines }}" ignore_errors : yes register : crunchify_processes - name : Force kill stuck processes ignore_errors : yes shell : "kill -9 {{ item }}" with_items : "{{ crunchify_processes.results | select('failed') | map(attribute='item') | list }}" |
여기 가능한 플레이북 파일은 모든 자바 프로세스를 가져와 간단한 kill -9
명령을 사용하여 종료합니다.
Ansible 플레이북 실행:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
bash1 . 2 $ ansible - playbook - i . / crunchify - hosts crunchify - grep - kill - process . yml PLAY [ crunchify ] ************************************************************************************************************************************************************** TASK [ Gathering Facts ] ******************************************************************************************************************************************************** ok : [ 3.16.83.84 ] TASK [ Get running processes list from remote host ] **************************************************************************************************************************** changed : [ 3.16.83.84 ] TASK [ Kill running processes ] ************************************************************************************************************************************************* changed : [ 3.16.83.84 ] = > ( item = 18174 ) failed : [ 3.16.83.84 ] ( item = 18342 ) = > { "changed" : true , "cmd" : "kill 18342" , "delta" : "0:00:00.002602" , "end" : "2019-05-10 15:20:36.957062" , "item" : "18342" , "msg" : "non-zero return code" , "rc" : 1 , "start" : "2019-05-10 15:20:36.954460" , "stderr" : "/bin/sh: 1: kill: No such process" , "stderr_lines" : [ "/bin/sh: 1: kill: No such process" ] , "stdout" : "" , "stdout_lines" : [ ] } failed : [ 3.16.83.84 ] ( item = 18344 ) = > { "changed" : true , "cmd" : "kill 18344" , "delta" : "0:00:00.002648" , "end" : "2019-05-10 15:20:38.479354" , "item" : "18344" , "msg" : "non-zero return code" , "rc" : 1 , "start" : "2019-05-10 15:20:38.476706" , "stderr" : "/bin/sh: 1: kill: No such process" , "stderr_lines" : [ "/bin/sh: 1: kill: No such process" ] , "stdout" : "" , "stdout_lines" : [ ] } . . . ignoring TASK [ wait_for ] *************************************************************************************************************************************************************** ok : [ 3.16.83.84 ] = > ( item = 18174 ) ok : [ 3.16.83.84 ] = > ( item = 18342 ) ok : [ 3.16.83.84 ] = > ( item = 18344 ) TASK [ Force kill stuck processes ] ********************************************************************************************************************************************* PLAY RECAP * ******************************************************************************************************************************************************************* 3.16.83.84 : ok = 4 changed = 2 unreachable = 0 failed = 0 |

확인하는 방법?
원격 호스트에서 grep 프로세스를 다시 시도하십시오.
1 2 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ ps - few | grep CrunchifyAlwaysRunningProgram ubuntu 18484 15069 0 15 : 22 pts / 0 00 : 00 : 00 grep -- color = auto CrunchifyAlwaysRunningProgram |
알다시피 목록에 process ID 18174
가 표시되지 않으며 실행 중인 Java 프로세스가 없습니다.
그게 다야
이것은 Java 프로세스를 grep하고 Ansible을 사용하여 종료하는 가장 간단한 방법입니다. 이 Ansible playbook
을 실행하는 데 문제가 있으면 알려주십시오.