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 have a script that is run by cron to create backup of a MySQL database and some files. After creating a tar ball and encrypting it with openSSH I have to put it on a remote ftp server. Following is the code for ftp part:


    cd ${DIRNAME}

    ftp -n ${HOST} <<END_SCRIPT
    quote USER ${USER}
    quote PASS ${PASSWD}
    cd backup
    lcd ${DIRNAME}
    put ${FILE}


If i directly run the script from command line > sudo ./ it runs smoothly and put the backup file on ftp, but when it is running from cron it has never put the file on ftp though other pre-ftp and post-ftp (like consolidating log file and emailing outcome) steps work fine. I am not able to get anything from any log files also or to pinpoint the cause.

NOTE:- Our ip does not change as we have paid plan.

share|improve this question
Don't cron mail you a report after task? You may find logs with the mail command. Did you checked that cron run with the right permissions? – M'vy Jun 16 '11 at 8:15
@M'vy cron is running the job as root. – TheVillageIdiot Jun 16 '11 at 9:02
What does the relevant section of crontab -l look like? – Kirk Jun 16 '11 at 12:22
sudo crontab -l ` . # m h dom mon dow command 0 22 * * * /usr/local/backups/` – TheVillageIdiot Jun 16 '11 at 15:38
up vote 3 down vote accepted

Use Curl's upload functionality instead:

curl --upload-file "$FILE" --user "$USER:$PASSWD" "ftp://$HOST/backup/"
share|improve this answer
again same problem works from command line but fails with cron :( – TheVillageIdiot Jun 17 '11 at 12:19
working from script also but got problem with same script on another system please see this if you can get any clue what is happening: – TheVillageIdiot Jun 20 '11 at 7:11
Try specifying path as cron uses a very restricted path by default: /usr/bin/curl ... – LatinSuD Jun 6 '14 at 12:48

Umm.. I see several points why it could fail.

  1. you don't know that ftp has opened a connection when you are sending input, but maybe it takes care of that for you (im not familiar with your ftp prog.. it could be anything afaik).
  2. you are not separating your ftp:// commands with semicolons or newlines - try adding \n or ;. again idk what ur app wants

  3. you are not checking that the file actually exists before trying to put it.

share|improve this answer
If it cannot take commands from batch, then how sudo ./ succeeds? The file does exists as it is still there after the script finishes. – TheVillageIdiot Jun 16 '11 at 15:28
Just because your ftp command doesn't crash or return an error when you feed it a string of of text, doesn't mean it accepts the tokens as arguments or commands. I mean, if you have seen somewhere that it can be done like that with the ftp command you are using then go ahead, otherwise I'm telling you you can't assume it will work because text protocols like ftp often return "ok" after doing authenticate, cd, etc. And you have to deal with that - a simple redirect from stdin does not deal with it, expect does. – Ярослав Рахматуллин Jun 17 '11 at 2:46
Next - your variables are probably failing. Where are ${LOCAL_HOST} and ${DATENAME} defined? cron executes commands under a "naked" environment, definetly not the same as sudo. Whats wrong with the standard ${HOSTNAME}? Nevermind.. Just verify that ${FILE} is there and that its not something like `myBack--.enc.tar.gz' like I already mentioned. – Ярослав Рахматуллин Jun 17 '11 at 2:55
@Ярослав Рахматуллин I have not putup whole script the variables are there and all the files are created and there only this ftp thing fricks up the whole show – TheVillageIdiot Jun 17 '11 at 12:19
so try running a different tool (another ftp) or run ftp with expect. Good luck. – Ярослав Рахматуллин Jun 17 '11 at 15:49

I had similar problem.The solution for me: to refer directly to the directories without variables like this:

lcd /mpla/mpla2/dir1
cd  /mpla3/mpla4/dir2

and it worked. Only this change. The quote USER $username and quote PASSWD $password were left as is but without the curly braces.

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.