{"id":19674254,"url":"https://github.com/zeroturnaround/zt-process-killer","last_synced_at":"2025-04-09T15:09:17.403Z","repository":{"id":30487366,"uuid":"34041484","full_name":"zeroturnaround/zt-process-killer","owner":"zeroturnaround","description":"ZeroTurnaround Process Killer","archived":false,"fork":false,"pushed_at":"2023-12-05T22:08:39.000Z","size":296,"stargazers_count":130,"open_issues_count":8,"forks_count":26,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-09T15:09:13.356Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zeroturnaround.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.txt","contributing":null,"funding":null,"license":"License.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-04-16T07:40:04.000Z","updated_at":"2024-12-21T09:22:39.000Z","dependencies_parsed_at":"2024-01-03T01:23:05.872Z","dependency_job_id":"dbbc3695-4926-428e-94a0-6a5863193864","html_url":"https://github.com/zeroturnaround/zt-process-killer","commit_stats":null,"previous_names":["zeroturnaround/zt-process"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroturnaround%2Fzt-process-killer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroturnaround%2Fzt-process-killer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroturnaround%2Fzt-process-killer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroturnaround%2Fzt-process-killer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zeroturnaround","download_url":"https://codeload.github.com/zeroturnaround/zt-process-killer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248055282,"owners_count":21040157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T17:17:42.193Z","updated_at":"2025-04-09T15:09:17.372Z","avatar_url":"https://github.com/zeroturnaround.png","language":"Java","funding_links":[],"categories":["项目","Projects"],"sub_categories":["流程","Processes"],"readme":"# ZT Process Killer\n\n### Continuous Integration \n[![Build Status](https://travis-ci.org/zeroturnaround/zt-process-killer.png)](https://travis-ci.org/zeroturnaround/zt-process-killer)\n\n### Quick Overview\n\nThe project was created in [ZeroTurnaround](http://zeroturnaround.com/) to have a stable base functionality of stopping running processes from Java.\nIt can stop processes started from Java (e.g. with [zt-exec](https://github.com/zeroturnaround/zt-exec)) as well as existing system processes based on their process ID (PID).\n\n### Installation\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.zeroturnaround/zt-process-killer/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.zeroturnaround/zt-process-killer)\n\nThe project artifacts are available in [Maven Central Repository](http://search.maven.org/#browse%7C636943745).\n\nTo include it in your maven project then you have to specify the dependency.\n\n```xml\n...\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.zeroturnaround\u003c/groupId\u003e\n    \u003cartifactId\u003ezt-process-killer\u003c/artifactId\u003e\n    \u003cversion\u003e1.11\u003c/version\u003e\n\u003c/dependency\u003e\n...\n```\n\n### Motivation\n\nIn Java [Process.destroy()](https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#destroy--) is ambiguous.\nOn Windows it terminates processes forcibly. On UNIX it terminates them gracefully.\nAs invoking **kill** commands from Java is errorprone it should have an advised API and good test coverage.\n\nWe had the following functional requirements:\n\n* check whether a process is alive\n* wait until a process has finished\n* terminate a process gracefully (by default disabled on Windows as it's unsupported - [WindowsProcess](https://github.com/zeroturnaround/zt-process-killer/blob/master/src/main/java/org/zeroturnaround/process/WindowsProcess.java))\n* terminate a process forcibly\n* get the process ID (PID) of running JVM\n\nand these non-functional requirements:\n\n* abstraction of processes regardless they are started from Java or not (use process ID) - [SystemProcess](https://github.com/zeroturnaround/zt-process-killer/blob/master/src/main/java/org/zeroturnaround/process/SystemProcess.java)\n* have process API similar to [java.lang.Process](https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html) \n* all waiting methods should have one version with timeout and another without it \n* stopping operation should be idempotent - if a process was already finished it is a success \n* separate generic behavior from process implementation - [ProcessUtil](https://github.com/zeroturnaround/zt-process-killer/blob/master/src/main/java/org/zeroturnaround/process/ProcessUtil.java)\n* support alternative implementations for stopping same process - [OrProcess](https://github.com/zeroturnaround/zt-process-killer/blob/master/src/main/java/org/zeroturnaround/process/OrProcess.java)\n* simple factory for creating process instances - [Processes](https://github.com/zeroturnaround/zt-process-killer/blob/master/src/main/java/org/zeroturnaround/process/Processes.java)\n* invoke Java 8 **java.lang.Process** methods if possible - [Java8Process](https://github.com/zeroturnaround/zt-process-killer/blob/master/src/main/java/org/zeroturnaround/process/Java8Process.java)\n\nLimitations:\n\n* As the process abstraction is also used for already running processes it can't access their streams or exit value.\n* The scope of this project is stopping single processes. However [WindowsProcess](https://github.com/zeroturnaround/zt-process-killer/blob/master/src/main/java/org/zeroturnaround/process/WindowsProcess.java)\nhas method **setIncludeChildren** to also stop child processes in case the root process is still running.\n\n### Examples\n\n* Get my PID\n\n```java\nint pid = PidUtil.getMyPid();\nSystem.out.println(\"My PID is \" + pid);\n```\n\n\u003chr/\u003e\n\n* Check whether a PID is alive\n\n```java\nint pid = Integer.parseInt(FileUtils.readFileToString(new File(\"pidfile\")));\nPidProcess process = Processes.newPidProcess(pid);\nboolean isAlive = process.isAlive();\nSystem.out.println(\"PID \" + pid + \" is alive: \" + isAlive);\n```\n\n\u003chr/\u003e\n\n* Check whether a process is alive\n\n```java\nProcess p = new ProcessBuilder(\"my-application\").start();\nJavaProcess process = Processes.newJavaProcess(p);\nboolean isAlive = process.isAlive();\nSystem.out.println(\"Process \" + process + \" is alive: \" + isAlive);\n```\n\n\u003chr/\u003e\n\n* Wait until an already started process has finished\n\n```java\nint pid = Integer.parseInt(FileUtils.readFileToString(new File(\"pidfile\")));\nPidProcess process = Processes.newPidProcess(pid);\nboolean finished = process.waitFor(10, TimeUnit.MINUTES);\nSystem.out.println(\"PID \" + pid + \" finished on time: \" + finished);\n```\n\n\u003chr/\u003e\n\n* Wait until the started process has finished\n\n```java\nProcess p = new ProcessBuilder(\"my-application\").start();\nJavaProcess process = Processes.newJavaProcess(p);\nboolean finished = process.waitFor(10, TimeUnit.MINUTES);\nSystem.out.println(\"Process \" + process + \" finished on time: \" + finished);\n```\n\n\u003chr/\u003e\n\n* Stop an already started process, timeout of 30 seconds for graceful stop, timeout of 10 seconds for forceful stop\n\n```java\nint pid = Integer.parseInt(FileUtils.readFileToString(new File(\"pidfile\")));\nPidProcess process = Processes.newPidProcess(pid);\nProcessUtil.destroyGracefullyOrForcefullyAndWait(process, 30, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);\n```\n\n\u003chr/\u003e\n\n* Stop the started process, timeout of 30 seconds for graceful stop, timeout of 10 seconds for forceful stop\n\n```java\nProcess p = new ProcessBuilder(\"my-application\").start();\nSystemProcess process = Processes.newStandardProcess(p);\nProcessUtil.destroyGracefullyOrForcefullyAndWait(process, 30, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeroturnaround%2Fzt-process-killer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeroturnaround%2Fzt-process-killer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeroturnaround%2Fzt-process-killer/lists"}