linux - 为什么这个 ftp get 命令在我的 shell 脚本中工作但在 cron 中失败

标签 linux shell ftp get 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 输出中缺少以下行。

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 字节。

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

最佳答案

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

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

关于linux - 为什么这个 ftp get 命令在我的 shell 脚本中工作但在 cron 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31031065/

相关文章:

linux - 如何将 sed 与需要转义的变量一起使用

linux - 如何在 Linux 中以名称 Bouvet-Island.png 递归复制同一目录中的所有文件 Norway.png

java - 使用 Apache FTPClient 通过代理连接到 FTP 服务器

java - 如何将java文件重定向到linux特定路径

linux - tslib 工具不会在屏幕上绘制任何内容

shell - 如何 grep 一个文件并获取接下来的 5 行

c++ - 满足退出条件时程序不退出?

arrays - bash 数组 + 如何在 bash 数组中设置变量

c++ - (Qt 或其他)在 Windows 桌面或 explorer.exe 上使用 http 或 ftp 协议(protocol)拖放 mime 类型 uri-list 的数据

unix - 相当于 ftp put 和 append in scp