OSX bash 脚本可以工作,但在 SFTP 上的 crontab 中失败
这个主题已经被详细讨论过,但是,我对这个主题有一个我无法破解的变体。两天过去了,我决定对社区进行 ping 操作。谢谢提前阅读..
执行。总结是我在 OS X 中有一个脚本,可以正常运行,并且手动执行时不会出现问题或错误。当我将脚本放入 crontab 中以每天运行时,它仍然会运行,但不会运行所有命令(特别是 SFTP)。
我已经阅读了足够多的帖子来解决环境问题,所以正如您将在下面看到的,我在出现 PATH 问题时硬引用了 SFTP 的位置...
我唯一能想到的是 IdentityFile 。注意:我将其放入我的用户而不是 root 的 crontab 中。所以我知道它应该在我创建的id_dsa.pub上拾取(并且已经与服务器共享)..
我并没有尝试执行任何时髦的期望命令来绕过密码等等。我不知道为什么当从 cron 运行时它会跳过 SFTP 行。
请参阅下面的代码..非常感谢您的帮助..谢谢
#!/bin/bash
export DATE=`date +%y%m%d%H%M%S`
export YYMMDD=`date +%y%m%d`
PDATE=$DATE
YDATE=$YYMMDD
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
FEED="~/Dropbox/"
USER="user"
HOST="host.domain.tld"
A="/tmp/5nPR45bH"
>${A}.file1${PDATE}
>${A}.file2${PDATE}
BYEbye ()
{
rm ${A}.file1${PDATE}
rm ${A}.file2${PDATE}
echo "Finished cleaning internal logs"
exit 0
}
echo "get -r *" >> ${A}.file1${PDATE}
echo "quit" >> ${A}.file1${PDATE}
eval mkdir ${FEED}${YDATE}
eval cd ${FEED}${YDATE}
eval /usr/bin/sftp -b ${A}.file1${PDATE} ${USER}@${HOST}
BYEbye
exit 0
this topic has been discussed at length, however, I have a variant on the theme that I just cannot crack. Two days into this now and decided to ping the community. THx in advance for reading..
Exec. summary is I have a script in OS X that runs fine and executes without issue or error when done manually. When I put the script in the crontab to run daily it still runs but it doesnt run all of the commands (specifically SFTP).
I have read enough posts to go down the path of environment issues, so as you will see below, I hard referenced the location of the SFTP in the event of a PATH issue...
The only thing that I can think of is the IdentityFile. NOTE: I am putting this in the crontab for my user not root. So I understand that it should pickup on the id_dsa.pub that I have created (and that has already been shared with the server)..
I am not trying to do any funky expect commands to bypass the password, etc. I dont know why when run from the cron that it is skipping the SFTP line.
please see the code below.. and help is greatly appreciated.. thx
#!/bin/bash
export DATE=`date +%y%m%d%H%M%S`
export YYMMDD=`date +%y%m%d`
PDATE=$DATE
YDATE=$YYMMDD
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
FEED="~/Dropbox/"
USER="user"
HOST="host.domain.tld"
A="/tmp/5nPR45bH"
>${A}.file1${PDATE}
>${A}.file2${PDATE}
BYEbye ()
{
rm ${A}.file1${PDATE}
rm ${A}.file2${PDATE}
echo "Finished cleaning internal logs"
exit 0
}
echo "get -r *" >> ${A}.file1${PDATE}
echo "quit" >> ${A}.file1${PDATE}
eval mkdir ${FEED}${YDATE}
eval cd ${FEED}${YDATE}
eval /usr/bin/sftp -b ${A}.file1${PDATE} ${USER}@${HOST}
BYEbye
exit 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是答案,只是对您的代码的评论。
处理带空格的文件名的方法是引用变量:
"$var"
--eval
不是正确的方法。养成引用所有变量的习惯,除非您特别想使用不引用的副作用。您不需要
导出
变量,除非您调用的命令希望在环境中看到它们。您不需要调用
date
两次,因为 YYMMDD 值是 DATE 的子字符串:YYMMDD="${DATE:0:6}"
< /p>$HOME
而不是~
。您从不使用“file2”临时文件 - 为什么要创建它?
由于您的 sftp 批处理文件非常简单,因此您实际上并不需要它的文件:
printf "%s\n" "get -r *" "退出" | sftp -b - "$USER@$HOST"
这是一个重写,大大缩短了:
Not an answer, just comments about your code.
The way to handle filenames with spaces is to quote the variable:
"$var"
--eval
is not the way to go. Get into the habit of quoting all variables unless you specifically want to use the side effects of not quoting.you don't need to
export
your variables unless there's a command you call that expects to see them in the environment.you don't need to call
date
twice because the YYMMDD value is a substring of the DATE:YYMMDD="${DATE:0:6}"
just a preference: I use
$HOME
over~
in a script.you never use the "file2" temp file -- why do you create it?
since your sftp batch file is pretty simple, you don't really need a file for it:
printf "%s\n" "get -r *" "quit" | sftp -b - "$USER@$HOST"
Here's a rewrite, shortened considerably: