Skip to content

Commit 46d211e

Browse files
committed
Godot script example for deleting processes in Windows OS
A Godot example script that shows how you can terminate running programs from your Windows OS. This can be useful for things like multiplayer testing where you want to automatically terminate all spawned clients on some event such as startup.
1 parent b5c7c3b commit 46d211e

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

default_env.tres

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[gd_resource type="Environment" load_steps=2 format=2]
2+
3+
[sub_resource type="ProceduralSky" id=1]
4+
5+
[resource]
6+
background_mode = 2
7+
background_sky = SubResource( 1 )

icon.png

3.23 KB
Loading

icon.png.import

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://icon.png"
13+
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0

main.gd

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
extends Node2D
2+
3+
onready var _endTaskButton = $EndTaskButton
4+
onready var _taskNameLineEdit = $TaskNameLineEdit
5+
6+
func _ready() -> void:
7+
pass
8+
9+
# Finds all tasks with the specified image name
10+
# which is the executable name as it shows up in task manager.
11+
func TerminateProcess(taskName):
12+
var output = []
13+
var error = OS.execute("tasklist", ["/fi", "imagename eq " + taskName, "/fo", "csv"], true, output)
14+
if error == 1:
15+
print("Failed to execute tasklist command!")
16+
return
17+
18+
var taskListData : Array = output[0].split("\n", false)
19+
20+
# Remove the headers
21+
taskListData.remove(0)
22+
23+
if taskListData.empty():
24+
print("Did not find a matching task to terminate.")
25+
return
26+
27+
for line in taskListData:
28+
var data = line.split(",")
29+
var pid = data[1]
30+
error = OS.execute("taskkill", ["/f", "/pid", pid], true)
31+
if error == 1:
32+
print("Failed to end task to pid: " + pid)
33+
else:
34+
print("Successfully ended task to pid: " + pid)
35+
36+
func _on_Button_pressed() -> void:
37+
if _taskNameLineEdit.text == "":
38+
OS.alert("Yo! Need to provide a task name first!")
39+
return
40+
41+
TerminateProcess(_taskNameLineEdit.text)

main.tscn

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[gd_scene load_steps=2 format=2]
2+
3+
[ext_resource path="res://main.gd" type="Script" id=1]
4+
5+
[node name="Main" type="Node2D"]
6+
script = ExtResource( 1 )
7+
8+
[node name="TaskNameLabel" type="Label" parent="."]
9+
margin_left = 27.0
10+
margin_top = 279.0
11+
margin_right = 101.0
12+
margin_bottom = 293.0
13+
text = "Task Name:"
14+
__meta__ = {
15+
"_edit_use_anchors_": false
16+
}
17+
18+
[node name="EndTaskButton" type="Button" parent="."]
19+
margin_left = 311.0
20+
margin_top = 274.0
21+
margin_right = 418.0
22+
margin_bottom = 297.0
23+
text = "End Task"
24+
__meta__ = {
25+
"_edit_use_anchors_": false
26+
}
27+
28+
[node name="TaskNameLineEdit" type="LineEdit" parent="."]
29+
margin_left = 103.0
30+
margin_top = 274.0
31+
margin_right = 306.0
32+
margin_bottom = 298.0
33+
__meta__ = {
34+
"_edit_use_anchors_": false
35+
}
36+
37+
[node name="WarningLabel" type="Label" parent="."]
38+
margin_left = 25.0
39+
margin_top = 27.0
40+
margin_right = 648.0
41+
margin_bottom = 228.0
42+
text = "WARNING! Please review the code and make sure it's going to work as expected for your specific environment. I make no promises in regards to the integrity and behavior of this code. Loss of data is possible if you end task to the wrong thing. Recommend stepping through all the code (including error handling) to make sure it's doing what you want.
43+
44+
For demo purposes, specify a taskname and click the EndTask button to terminate ALL processes with that name.
45+
46+
A simple test scenario is to launch and terminate one or more notepad.exe processes. Make sure to save and close any that are already open first! Do specify the file extension in the Task Name field as well. Example: notepad.exe
47+
48+
Some errors output to the debug console so check there if nothing happens."
49+
autowrap = true
50+
__meta__ = {
51+
"_edit_use_anchors_": false
52+
}
53+
[connection signal="pressed" from="EndTaskButton" to="." method="_on_Button_pressed"]

project.godot

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=4
10+
11+
_global_script_classes=[ ]
12+
_global_script_class_icons={
13+
14+
}
15+
16+
[application]
17+
18+
config/name="godot-script-endtask"
19+
run/main_scene="res://main.tscn"
20+
config/icon="res://icon.png"
21+
22+
[rendering]
23+
24+
environment/default_environment="res://default_env.tres"

0 commit comments

Comments
 (0)