2

I can create ssh connection from the command line with this command(without password):

ssh -R 9900:localhost:22 user@host

I need to run this command whenever the system reboots, so I put it in a bash script and added this line to crontab:

@reboot /home/me/script.sh > output

But in output I get this error: ssh: Could not resolve hostname myhost: Name or service not known

I think it's because this script starts before internet connection is established. I put a sleep(180) before the ssh command, but it didn't help. Any suggestion?

0

Ideally this shouldn't be done using cron, this is what your init system is for. You could create a service that is dependent on your network state.

However, this should work if you are intent on using cron:

#!/bin/sh

host="foo.bar.com"

# Wait for host's SSH server to become accessible
while ! ssh "user@$host" exit >/dev/null 2>&1; then
    sleep 1
fi

ssh -R 9900:localhost:22 "user@$host"
  • Thanks, but didn't work(I replaced then and fi with do and done).This one also works in command line, but not in cron – user39463 May 19 '13 at 13:08
  • @user39463 Are you sure you can log in without a password? Did you set up your SSH agent properly? – Chris Down May 19 '13 at 13:38

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.