Created
March 3, 2025 04:35
-
-
Save ucalyptus/d89a5d25d9c87450f1aafbb217fd76aa to your computer and use it in GitHub Desktop.
So you forgot attaching a bash / python process into a tmux session? this script should have you covered. Make sure you install reptyr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to find the parent process | |
find_parent_process() { | |
local child_pid=$1 | |
local parent_pid=$(ps -o ppid= -p $child_pid | tr -d ' ') | |
echo $parent_pid | |
} | |
# Check if process ID was provided | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 <process_id>" | |
exit 1 | |
fi | |
PID=$1 | |
# Get the parent process (likely the bash script) | |
PARENT_PID=$(find_parent_process $PID) | |
# Check if the process exists | |
if ! ps -p $PARENT_PID > /dev/null; then | |
echo "Parent process $PARENT_PID not found." | |
exit 1 | |
fi | |
# Check if tmux is installed | |
if ! command -v tmux &> /dev/null; then | |
echo "tmux is not installed. Please install it first." | |
exit 1 | |
fi | |
# Check if reptyr is installed | |
if ! command -v reptyr &> /dev/null; then | |
echo "reptyr is not installed. Please install it first." | |
exit 1 | |
fi | |
# Set ptrace_scope to 0 if needed (requires sudo) | |
current_scope=$(cat /proc/sys/kernel/yama/ptrace_scope 2>/dev/null) | |
if [ "$current_scope" != "0" ]; then | |
echo "Setting ptrace_scope to 0 (requires sudo)" | |
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope | |
fi | |
# Create a new tmux session if not already in one | |
if [ -z "$TMUX" ]; then | |
echo "Creating new tmux session..." | |
tmux new-session -d -s "process_$PARENT_PID" | |
# Send the reptyr command to the tmux session | |
tmux send-keys -t "process_$PARENT_PID" "reptyr -T $PARENT_PID" C-m | |
# Attach to the tmux session | |
echo "Attaching to tmux session..." | |
tmux attach-session -t "process_$PARENT_PID" | |
else | |
# Already in a tmux session, just run reptyr | |
echo "Already in tmux, running reptyr on parent process $PARENT_PID..." | |
reptyr -T $PARENT_PID | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment