简体   繁体   中英

Why does this ftp get command work in my shell script but fail in cron

I have a shell script that I can run as a certain user to get a file from a server. I want to run it at a specific time. It works when I run it manually as that user with the command:

sudo -H -u myUser bash -c /absolute/path/to/my/script/myScript.sh

I wanted to run the script as that user in general cron so I added the line to /etc/crontab which also works:

16 10   * * *   myUser /absolute/path/to/my/script/myScript.sh >> /tmp/logOfMyScript.sh.txt

The script itself is a basic (I mean basic!) ftp script and it works when I run it:

#!/bin/bash
filename="myBinaryFile.dta"
hostname="ftp.mydomain.com"
username="myusername"
password="mypassword"
ftp -ivn $hostname <<EOF
quote USER $username
quote PASS $password
binary
get $filename
quit
EOF

The results when I run it manually with the command (above)are perfect:

[sudo] password for me: 
Connected to myDomain.com.
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 2 of 50 allowed.
220-Local time is now 07:55. Server port: 21.
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
331 User myUser OK. Password required
230 OK. Current restricted directory is /
200 TYPE is now 8-bit binary
local: myBinaryFile.dta remote: MyBinaryFile.dta
200 PORT command successful
150-Connecting to port 39317
150 1161.4 kbytes to download
226-File successfully transferred
226 2.476 seconds (measured here), 469.07 Kbytes per second
1189253 bytes received in 2.63 secs (442.0 kB/s)
221-Goodbye. You uploaded 0 and downloaded 1162 kbytes.
221 Logout.

comparing the log of my manual running of the script and the output from cron, the following lines are MISSING from cron output.

200 PORT command successful
150-Connecting to port 39317
150 1161.4 kbytes to download
226-File successfully transferred
226 2.476 seconds (measured here), 469.07 Kbytes per second
1189253 bytes received in 2.63 secs (442.0 kB/s)

and the worst part is the last line says

221-Goodbye. You uploaded 0 and downloaded 0 kbytes.

Anybody know why the port isn't called in cron? Is there a variable that needs to be set. The port is 21 the normal port and it indicates as fine in the log of the cron script.

You'd be better off not using ftp for anything, especially not non-interactive use. wget can download files over ftp in a robust, flexible and predictable way:

wget --user="userName" --password="userPassword" \
    "ftp://ftp.myDomain.com/myBinaryFile.dta"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM