Skip to content

Commit 8be31b5

Browse files
authored
Added kill process script (#347)
* Added kill process MLC script
1 parent 2c45516 commit 8be31b5

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

script/kill-process/customize.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from mlc import utils
2+
import os
3+
import subprocess
4+
5+
6+
def generate_kill_command(env):
7+
kill_cmd = ""
8+
9+
if env.get("MLC_KILL_PROCESS_GROUP"):
10+
if env.get("MLC_KILL_PROCESS_ID"):
11+
process_id = env["MLC_KILL_PROCESS_ID"]
12+
# Find process group and kill all processes in it
13+
kill_cmd = f"pkill -g $(ps -o pgid= -p {process_id} | tr -d ' ')"
14+
15+
elif env.get("MLC_KILL_PROCESS_NAME"):
16+
process_name = env["MLC_KILL_PROCESS_NAME"]
17+
# Find process group of the first matching process name and kill
18+
# all in that group
19+
kill_cmd = f"pkill -g $(pgrep -o {process_name} | xargs ps -o pgid= -p | tr -d ' ')"
20+
21+
else:
22+
if env.get("MLC_KILL_PROCESS_ID"):
23+
process_id = env["MLC_KILL_PROCESS_ID"]
24+
kill_cmd = f"kill {process_id}" # Kill a single process by ID
25+
26+
elif env.get("MLC_KILL_PROCESS_NAME"):
27+
process_name = env["MLC_KILL_PROCESS_NAME"]
28+
# Kill all processes matching the name
29+
kill_cmd = f"pkill {process_name}"
30+
31+
env["MLC_RUN_CMD"] = kill_cmd if kill_cmd else "echo 'No valid input provided'"
32+
33+
34+
def preprocess(i):
35+
36+
env = i['env']
37+
state = i['state']
38+
os_info = i['os_info']
39+
40+
generate_kill_command(env)
41+
42+
return {'return': 0}
43+
44+
45+
def postprocess(i):
46+
47+
env = i['env']
48+
state = i['state']
49+
50+
os_info = i['os_info']
51+
52+
return {'return': 0}

script/kill-process/meta.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
alias: kill-process
2+
automation_alias: script
3+
automation_uid: 5b4e0237da074764
4+
category: MLC Script Template
5+
deps: []
6+
new_env_keys: []
7+
new_state_keys: []
8+
post_deps: []
9+
posthook_deps: []
10+
prehook_deps: []
11+
tags:
12+
- kill
13+
- process
14+
tests:
15+
run_inputs: []
16+
uid: 27b5651692454cb6
17+
input_mapping:
18+
group: MLC_KILL_PROCESS_GROUP
19+
pid: MLC_KILL_PROCESS_ID
20+
pname: MLC_KILL_PROCESS_NAME
21+
22+

script/kill-process/run.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
function exit_if_error() {
3+
test $? -eq 0 || exit $?
4+
}
5+
6+
function run() {
7+
echo "Running: "
8+
echo "$1"
9+
echo ""
10+
if [[ ${MLC_FAKE_RUN} != 'yes' ]]; then
11+
eval "$1"
12+
exit_if_error
13+
fi
14+
}
15+
16+
#Add your run commands here...
17+
run "$MLC_RUN_CMD"

0 commit comments

Comments
 (0)