Shutdown NOT super-user

bashscriptshutdownsshterminal

I'm trying to shutdown Macs after 24 hours of runtime with this script:

#!/bin/bash
BOOT_TIME=$(sysctl -n kern.boottime | sed -e 's/.* sec = \([0-9]*\).*/\1/')
CURR_TIME=$(date +%s)
MAX_UPDAYS=1 #Days

DAYS_UP=$(( ( $CURR_TIME - $BOOT_TIME) / 86400 ))
    if [ $DAYS_UP -ge ${MAX_UPDAYS} ];then
        echo Mac is going to shutdown 
        shutdown -h now
    else
        echo No shutdown needed
fi

The file name is ShutdownUPTIME.sh.
Now when I try to type in:

sudo ssh ADMIN@macxxx 'bash -s' < ./documents/ShutdownUPTIME.sh

it wants a password. I type that in and then the script runs. The only thing that pops up though is:

shutdown: NOT super-user

The user is in the sudoers file as ALL=(ALL) ALL on both Macs.
Any ideas?

Best Answer

You are running ssh command locally as root and the remote bash (hence the whole script) as the user ADMIN@macxxx (without switching to root on the remote server).

You must precede either bash or shutdown with sudo (provided ADMIN has passwordless sudo permissions for shutdown or all commands on macxxx machine).

So either:

ssh ADMIN@macxxx 'sudo bash -s' < ./documents/ShutdownUPTIME.sh

Or:

...
if [ $DAYS_UP -ge ${MAX_UPDAYS} ];then
    echo Mac is going to shutdown 
    sudo shutdown -h now
else
...