繁体   English   中英

为什么此ftp get命令在我的shell脚本中起作用但在cron中失败

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

我有一个Shell脚本,可以作为特定用户运行以从服务器获取文件。 我想在特定时间运行它。 当我使用以下命令以该用户身份手动运行它时,它可以工作:

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

我想以一般cron的用户身份运行脚本,所以我在/ etc / crontab中添加了该行,该行也可以运行:

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

该脚本本身是一个基本的(我是说基本的!)ftp脚本,在我运行它时可以运行:

#!/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

当我使用命令(上面)手动运行它时,结果是完美的:

[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.

比较我手动运行脚本的日志和cron的输出,以下几行是cron输出的MISSING。

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-再见。 您上传了0,下载了0 KB。

有人知道为什么在cron中没有调用该端口吗? 是否有需要设置的变量。 该端口是普通端口21,在cron脚本的日志中表示正常。

您最好不要将ftp用于任何用途,尤其是非交互式用途。 wget可以以健壮,灵活和可预测的方式通过ftp下载文件:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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