简体   繁体   English

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

[英]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. 我有一个Shell脚本,可以作为特定用户运行以从服务器获取文件。 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: 我想以一般cron的用户身份运行脚本,所以我在/ etc / crontab中添加了该行,该行也可以运行:

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: 该脚本本身是一个基本的(我是说基本的!)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

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. 比较我手动运行脚本的日志和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)

and the worst part is the last line says 最糟糕的是最后一行说

221-Goodbye. 221-再见。 You uploaded 0 and downloaded 0 kbytes. 您上传了0,下载了0 KB。

Anybody know why the port isn't called in cron? 有人知道为什么在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. 该端口是普通端口21,在cron脚本的日志中表示正常。

You'd be better off not using ftp for anything, especially not non-interactive use. 您最好不要将ftp用于任何用途,尤其是非交互式用途。 wget can download files over ftp in a robust, flexible and predictable way: 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