Rename run_with_sudo.sh to python_sudo.sh

This commit is contained in:
Mike Geppert 2025-07-27 15:38:42 -05:00
parent 5c2821b74d
commit 44b4ad0c04

54
python_sudo.sh Executable file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env bash
# Wrapper script to run Python scripts with sudo privileges
# Works with both bash and zsh shells
# Usage: ./run_with_sudo.sh <python_script.py> [arguments...]
# Display usage information if no arguments provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <python_script.py> [arguments...]"
echo "This script runs the specified Python script with sudo privileges."
echo "The user will be prompted for the root password."
exit 1
fi
# Get the Python script path (first argument)
PYTHON_SCRIPT="$1"
shift # Remove the first argument, leaving only the script arguments
# Check if the Python script exists
if [ ! -f "$PYTHON_SCRIPT" ]; then
echo "Error: Python script '$PYTHON_SCRIPT' not found."
exit 1
fi
# Check if the Python script is executable
if [ ! -x "$PYTHON_SCRIPT" ] && [[ "$PYTHON_SCRIPT" == *.py ]]; then
echo "Warning: Python script '$PYTHON_SCRIPT' is not executable."
echo "Consider running: chmod +x $PYTHON_SCRIPT"
fi
# Determine the Python interpreter to use
# First try python3, then fall back to python if python3 is not available
if command -v python3 &>/dev/null; then
PYTHON_CMD="python3"
elif command -v python &>/dev/null; then
PYTHON_CMD="python"
else
echo "Error: Neither python3 nor python is available on this system."
exit 1
fi
echo "Running '$PYTHON_SCRIPT' with sudo privileges..."
echo "You may be prompted for your password."
# Run the Python script with sudo, passing all remaining arguments
sudo "$PYTHON_CMD" "$PYTHON_SCRIPT" "$@"
# Check the exit status
EXIT_STATUS=$?
if [ $EXIT_STATUS -ne 0 ]; then
echo "The Python script exited with status code: $EXIT_STATUS"
exit $EXIT_STATUS
fi
exit 0