我有一个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千字节。
有人知道为什么在cron中没有调用端口?是否需要设置变量。端口是普通端口的21,它在cron脚本的日志中表示正常。
答案 0 :(得分:2)
最好不要将ftp
用于任何事情,尤其是非交互式使用。 wget
可以通过强大,灵活和可预测的方式在ftp上下载文件:
wget --user="userName" --password="userPassword" \
"ftp://ftp.myDomain.com/myBinaryFile.dta"