Sobes.tech
Junior

How to write a shell script so that it always runs as the root user?

sobes.tech AI

Answer from AI

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root."
   exit 1
fi

# Main script code that requires root privileges

echo "The script is running as root."