为什么这个 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.
比较我的脚本手册 运行ning 的日志和 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 KB。
有人知道为什么在 cron 中不调用该端口吗?是否有需要设置的变量。端口是正常端口 21,它在 cron 脚本的日志中表示正常。
您最好不要将 ftp
用于任何用途,尤其是非交互式用途。 wget
可以通过 ftp 以可靠、灵活和可预测的方式下载文件:
wget --user="userName" --password="userPassword" \
"ftp://ftp.myDomain.com/myBinaryFile.dta"
我有一个 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.
比较我的脚本手册 运行ning 的日志和 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 KB。
有人知道为什么在 cron 中不调用该端口吗?是否有需要设置的变量。端口是正常端口 21,它在 cron 脚本的日志中表示正常。
您最好不要将 ftp
用于任何用途,尤其是非交互式用途。 wget
可以通过 ftp 以可靠、灵活和可预测的方式下载文件:
wget --user="userName" --password="userPassword" \
"ftp://ftp.myDomain.com/myBinaryFile.dta"