Trying to remote shutdown Ubuntu computer on same lan using a Mac computer. If I open Terminal on mac, I CAN shut down the Ubuntu by using: ssh testbox@, then typing in the password, then typing sudo poweroff, typing in password again. This DOES work.

The problem is I need to do this by using Mac Applescript. (so I can create a Calendar Event and have it run at a certain time of day after backups are done)

NOTE: I have successfully created an Applescript that Wakeonlan turns ON the Ubuntu at a specific time of the day:

tell application "Terminal"
    set currentTab to do script
    ("ssh testbox@;")
    delay 6
    do script ("~/bin/wakeonlan 00:")
end tell

This works well, sends the wakeonlan and starts Ubuntu, but I can't figure out how to do the same thing to SHUTDOWN Ubuntu computer? I've tried just about everything? All the posts I find only show how to do this from Ubuntu Terminal, NOT from Mac AppleScript put in Mac's Terminal?

  • 1
    This seems like off-topic since asks about mac code... – schrodigerscatcuriosity Sep 10 '20 at 1:05
  • Is the Ubuntu machine? If so, your WOL script does not do what you may think it does. If not, why do you need to send the WOL packet from a different machine? – danzel Sep 10 '20 at 14:37
  • Danzel, yes the is the Ubuntu machine on the lan. The WOL script above works well. I send WOL from another machine (Mac) is so it can receive backup files at a specific time of day from other machine. Need to figure out how to shut the Ubuntu machine off after it's done? 30 min after startup would do. Mac has ability to send Shell Scripts but I don't know how to write a Shell Script 1. Open Application "Terminal", 2. ssh into Ubuntu and give password, 3. Issue command to "sudo poweroff" machine. If someone knows how to write Shell Script, I would really appreciate it!! – N.T. Sep 10 '20 at 19:44
  • I just noticed that I don't even need to ssh or the IP on lan. Now it just says Tell application "Terminal" to set currentTab to do script ("~/bin/wakeonlan 00:11:22:33:44:5C) (I put in the real address for the ethernet card) and it sends the magic packet WOL and it wakes it up. However, I still can't get it to "sudo poweroff". Stumped here! – N.T. Sep 11 '20 at 19:11

If you are going to try to do this all via apple script, then “cats” comment seems on point: you need an Apple forum.

But it seems to me that your real problem is how to avoid being challenged for passwords. And ssh-copy-id was invented precisely for that purpose. If you are willing to give it a go...

On the target Ubuntu host:

Create a dedicated user for this purpose

sudo adduser --force-badname SandMan

Next, edit /etc/sudoers to allow SandMan to execute "sudo /sbin/shutdown ..." unchallenged.

sudo visudo

(This opens "nano" as the editor by default - you'll have to muddle through on your own; I change all mine over to vi.) Insert the following before the includedir directive at the end:

%SandMan ALL=NOPASSWD: /sbin/shutdown

Save the file

You should now be able to log into your host using the SandMan user and execute sudo /sbin/shutdown --help without being challenged for the password.

To allow you to execute the shutdown directly from your MACs terminal window try (from your MAC terminal):

ssh-keygen -t rsa -b 2048 -f ~/.ssh/SandMan_rsa
ssh-copy-id -i ~/.ssh/SandMan_rsa SandMan@

This is good syntax for Ubuntu 20.04, but I am unsure this will work on the MAC without tweaking. But if it does work, you should then be able to shut down your target from the MAC terminal with:

ssh -i ~/.ssh/SandMan_rsa SandMan@ /sbin/shutdown -h now


I finally figured it out. Here is what works:

tell application "Terminal"
  do script ("ssh testbox@")
  delay 6
  do script "password" in front window
  delay 7
  do script "sudo poweroft” in front window
  delay 5
  do script "password" in front window
end tell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.