Using Terminal to shut down a computer

I have a blind server (Macmini) and sometimes a process might hang.  It is set to shut down at hour x and start at hour y.  However if a process is hanging this will not happen.  I want to use Apple Script to run a script after hour x to force the Mac to shut down. 

 

Now I know “sudo shutdown -r now” will do so but it needs a password.  I could script the password in the Apple Script but would rather avoid this.  It there a way to bypass it all and shut down the Mac completely.


Thanks


Posted on Jun 16, 2020 11:28 PM

Reply
6 replies

Jun 17, 2020 7:20 AM in response to ChangeAgent

You could create a launchctl entry that runs as root, and issues the shutdown command. No 'sudo' needed. Google creating launchctl .plists and setting them up so they are always run.


Or a Unix style 'cron' job under the root account. DO NOT attempt this if you are not comfortable with cron, nor with the Unix style editors (you can become root using sudo su - )


Jun 17, 2020 7:33 AM in response to ChangeAgent

You could try tweaking the /etc/sudoers file to allow /sbin/shutdown to be run by your userid without a password:


# User privilege specification
root    ALL=(ALL) ALL
%admin  ALL=(ALL) ALL
X Y = NOPASSWD: /sbin/shutdown


where X is your user name, and Y is the machine name (without .local).


Use:

sudo visudo


and it will automatically open the /etc/sudoers file in the vi editor. Type a colon, and x to save and exit after your changes.


All you need in an AppleScript is this line when run from your user login where you are also the administrator:


do shell script "sudo /sbin/shutdown -r now"


and as tested on my Mojave machine, a reboot occurs. Sudo will ask for no password when running shutdown.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using Terminal to shut down a computer

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.