My home computer, a Mac running OS X 10.8.5, normally accepts SSH connections over the Internet (I have port forwarded) but is asleep right now. It sometimes may wake up for various reasons while I am away from home. I want to send it some SSH commands but can't sit and keep trying until it's awake.

Is there a simple, non-AppleScript way to delay an SSH connection, password authentication, and command input until the host computer responds? I'm using a Mac away from home, but I'd like to know a way to do this on any Unix-based OS. Normally, it times out after a minute or so of trying to connect.

I know that in most terminal emulators, you can type commands while blocking processes are running, and they'll execute once those processes are finished. An example is typing git push foo right after git commit -a -m "foo" while git commit -a -m "foo" is still running; git will push once it's done committing. I thought there might be some trick to doing something like this with ssh through a terminal emulator.

1 Answer 1


You can check with ping if your machine comes up and then fire off one or several ssh commands. Note that you will need to setup certificate-based authentication on your host. Here's a little script based on GNU/Linux' BASH but I guess you can convert it to whatever Mac OS X is using.

while [ $hostup -ne 1 ]; do
    ping -c 4 <hostname or IP>
    if [ $? -eq  0 ]; then
        ssh username@hostname <command to execute>

Only one command per SSH line and if you want to use parameters, you have to quote your command. If you are going to execute the same commands all the time, maybe a script on your home machine is in order.

  • This is pretty good, but the main problem is that I don't want to use certificate-based authentication. sshpass allows the user to specify the SSH password in the command, so that's a way to get around it.
    – sudo
    Jul 8, 2014 at 7:01
  • Glad that I could help. I didn't know about sshpass, so I've learned something new. Thank you.
    – bjanssen
    Jul 9, 2014 at 5:44
  • Unfortunately, sshpass is not in the official Homebrew repo because they don't trust us with it, and the unofficial one is outdated. I think it's in MacPorts, so I'll try that once I get rid of Homebrew.
    – sudo
    Jul 9, 2014 at 16:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .