Ansible –リモートホストで実行されているLinuxプロセスをGrep(ps -few)して強制終了する方法は?
公開: 2022-02-24Ansibleはかなり素晴らしいシステム管理ツールです。 過去数週間に、リモートホストでファイルをコピーする方法、リモートホストでコマンドを実行する方法、リモートホストにJava、Pythonをインストールする方法など、Ansibleに関する多数の記事を公開しました。
このチュートリアルでは、リモートホストで実行されているJavaプロセスをgrepし、単純な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 |
- リモートホストIPを持つファイル
crunchify-hosts
ファイルを作成します - ファイル
crunchify-grep-kill-process.yml
を作成し、grepおよびkilljavaプロセスのansibleタスクを使用します - コマンドを実行します: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 }}" |
ここで、ansibleプレイブックファイルはすべてのJavaプロセスを取得し、単純なkill -9
-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
が表示されず、実行中のJavaプロセスもありません。
それでおしまい。
これは、Ansibleを使用してJavaプロセスをgrepして強制終了する最も簡単な方法です。 このAnsible playbook
の実行で問題が発生した場合はお知らせください。