#!/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 " 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