Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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"
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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