Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to know how to setup a cron job that automatically connects to the remote server and change the directory and get all the files in that directory to local

i think i have to use sftp but i saw some commands called "spawn" in the some shell scripts and i am confused what this will do and what is for ?

spawn  sftp user@ipaddress
cd xxx/inbox
mget *

will this work in the context of the downloading remote directory ?

share|improve this question
it creates a new process:… I don't even have that command. You should run spawn -v; spawn -V spawn --version; spawn --usage; spawn --help; spawn -h; spawn -help; help spawn; man spawn and see if you can find any information about where that command really comes from. Or at least tell us what OS you are on. It would also be useful to know which ftp clients you have installed (ncftp, lftp, ftp, etc) and if you need to authenticate against the server with a username and password. – Ярослав Рахматуллин Oct 17 '12 at 1:27
Which shell is it? Does the first line of the script start with #!? Could you please tell us what is in the first line? Did you find the lines you show here somewhere like this or did you put them together from multiple sources? The spawn command looks like a command of an expect script but the following two commands look like sftp commands (i.e. sftp batch). – pabouk Jun 25 '13 at 14:09
Did you finally manage to setup the cron job? – pabouk Oct 15 '13 at 10:04
not really i am doing it on amazon for now, thanks for your answer i will try that and see.. thanksagain.. – user1179459 Oct 16 '13 at 0:06

In your case spawn is most probably a command of expect scripting language which allows automation of interactive program operations. In such a case spawn runs an external command from the expect script. Your script example is missing a shebang sequence (first line starting by #!) indicating the expect interpreter and as such will not be interpreted by expect when executed directly.

Password authentication with sftp is limited to the interactive mode. To control sftp in interactive mode you can use for example the following expect script:

#!/usr/bin/env expect
set timeout 20    # max. 20 seconds waiting for the server response

set user username
set pass your-pass
set host the-host-address
set dir  server-dir

spawn sftp $user@$host
expect assword:

send "$pass\r"
expect sftp>

send "cd $dir\r"
expect sftp>

send "mget *\r"
expect sftp>

send "exit\r"
expect eof

Another possibility is to use the public key authentication which is also more secure (see the procedure). In such a case you can simply use sftp directly in batch mode:


sftp -b - "$user@$host" <<+++EOF+++
cd "$dir"
mget *
share|improve this answer

Your Answer


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.