Use $ENV{SUDO_USER} instead of getlogin() to identify the user of the script.