Ansible - วิธี Grep (ps -few) และฆ่ากระบวนการ linux ที่ทำงานบน Remote Host?
เผยแพร่แล้ว: 2022-02-24Ansible เป็นเครื่องมือดูแลระบบที่น่าทึ่งมาก เราได้เผยแพร่บทความจำนวนหนึ่งใน Ansible ในช่วงไม่กี่สัปดาห์ที่ผ่านมาเกี่ยวกับวิธีคัดลอกไฟล์บนโฮสต์ระยะไกล วิธีดำเนินการคำสั่งบนโฮสต์ระยะไกล วิธีติดตั้ง Java, Python บนโฮสต์ระยะไกล และอื่นๆ
ในบทช่วยสอนนี้ เราจะพูดถึงวิธีการ grep java process ที่ทำงานบนรีโมตโฮสต์และฆ่ากระบวนการรีโมตนั้นโดยใช้ playbook ansible อย่างง่าย
นี่คือขั้นตอนที่เราจะทำในบทช่วยสอนนี้:
- บนรีโมตโฮสต์ ให้รัน 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 |
- สร้างไฟล์
crunchify-hosts
ไฟล์ซึ่งมี IP โฮสต์ระยะไกล - สร้างไฟล์
crunchify-grep-kill-process.yml
ด้วย ansible task สำหรับ grep และ kill java process - รันคำสั่ง: ansible-playbook -i ./crunchify-hosts crunchify-grep-kill-process.yml
- ตรวจสอบผลลัพธ์บนคอนโซลเทอร์มินัล macOS
ไฟล์ crunchify-hosts
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 }}" |
ไฟล์ playbook ansible นี้ได้รับกระบวนการ java ทั้งหมด โดยฆ่ามันโดยใช้คำสั่ง kill -9
อย่างง่าย

ดำเนินการ Ansible Playbook:
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
ในรายการ และไม่มีกระบวนการของจาวาที่ทำงานอยู่
แค่นั้นแหละ.
นี่เป็นวิธีที่ง่ายที่สุดในกระบวนการ grep Java และฆ่าโดยใช้ Ansible แจ้งให้เราทราบหากคุณประสบปัญหาในการใช้งาน Ansible playbook
นี้