How to hinder root from running a script
Similar to the other answers, but in the direction you wanted.
if [[ $EUID -eq 0 ]]; then
echo "This script must NOT be run as root" 1>&2
exit 1
fi
Alternatively, you can use sudo
within the script to force execution as the non-privileged user using the -u
flag to specify the user to run as. I don't use Glassfish, but here's a proto-example pseudo script.
#!/bin/bash
if [ $1 == "start" ]; then
sudo -u nobody /usr/bin/glassfish
fi
Hopefully you get the idea. Sorry I don't really know what the script looks like, or the name of the non-privileged user.
@Tshepang has the right idea. I don't know which is most portable, but here are other ways to detect whether the user is root:
id -u
$UID
Alternatively, you can simply force it to run as another user with the same parameters:
if [[ "$USER" != appuser ]]
then
exec sudo -u appuser "$0" "$@"
fi