Skip to content

Added kill process script #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions script/kill-process/customize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from mlc import utils
import os
import subprocess


def generate_kill_command(env):
kill_cmd = ""

if env.get("MLC_KILL_PROCESS_GROUP"):
if env.get("MLC_KILL_PROCESS_ID"):
process_id = env["MLC_KILL_PROCESS_ID"]
# Find process group and kill all processes in it
kill_cmd = f"pkill -g $(ps -o pgid= -p {process_id} | tr -d ' ')"

elif env.get("MLC_KILL_PROCESS_NAME"):
process_name = env["MLC_KILL_PROCESS_NAME"]
# Find process group of the first matching process name and kill
# all in that group
kill_cmd = f"pkill -g $(pgrep -o {process_name} | xargs ps -o pgid= -p | tr -d ' ')"

else:
if env.get("MLC_KILL_PROCESS_ID"):
process_id = env["MLC_KILL_PROCESS_ID"]
kill_cmd = f"kill {process_id}" # Kill a single process by ID

elif env.get("MLC_KILL_PROCESS_NAME"):
process_name = env["MLC_KILL_PROCESS_NAME"]
# Kill all processes matching the name
kill_cmd = f"pkill {process_name}"

env["MLC_RUN_CMD"] = kill_cmd if kill_cmd else "echo 'No valid input provided'"


def preprocess(i):

env = i['env']
state = i['state']
os_info = i['os_info']

generate_kill_command(env)

return {'return': 0}


def postprocess(i):

env = i['env']
state = i['state']

os_info = i['os_info']

return {'return': 0}
22 changes: 22 additions & 0 deletions script/kill-process/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
alias: kill-process
automation_alias: script
automation_uid: 5b4e0237da074764
category: MLC Script Template
deps: []
new_env_keys: []
new_state_keys: []
post_deps: []
posthook_deps: []
prehook_deps: []
tags:
- kill
- process
tests:
run_inputs: []
uid: 27b5651692454cb6
input_mapping:
group: MLC_KILL_PROCESS_GROUP
pid: MLC_KILL_PROCESS_ID
pname: MLC_KILL_PROCESS_NAME


17 changes: 17 additions & 0 deletions script/kill-process/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
function exit_if_error() {
test $? -eq 0 || exit $?
}

function run() {
echo "Running: "
echo "$1"
echo ""
if [[ ${MLC_FAKE_RUN} != 'yes' ]]; then
eval "$1"
exit_if_error
fi
}

#Add your run commands here...
run "$MLC_RUN_CMD"
Loading