bash Shell 脚本和 CRON 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5064518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script and CRON problems
提问by Pete
I've written a backup script for our local dev server (running Ubuntu server edition 9.10), just a simple script to tar & gzip the local root and stick it in a backup folder. It works fine when I run :
我为我们的本地开发服务器(运行 Ubuntu 服务器版本 9.10)编写了一个备份脚本,只是一个简单的脚本来对本地根目录进行 tar & gzip 并将其粘贴到备份文件夹中。当我运行时它工作正常:
$ bash backups.sh
but it wont work when I run it through crontab.
但是当我通过 crontab 运行它时它不会工作。
59 23 * * * bash /home/vnc/backups/backup.sh >> /home/vnc/backups/backup.log 2>
I get the error message
我收到错误信息
/bin/sh: cannot create : nonexistent
The script makes the tar.gz in the folder it is running from (/home/user1), but then tries to copy it to a mounted share (/home/backups, which is really 192.168.0.6/backups) from a network drive, via using fstab. The mounted share has permissions 777 but the owner and group are different to those running the script. I'm using bash to run the script instead of sh to get around another issue I've had in the past with "bad substitution" errors
该脚本将 tar.gz 放在它运行的文件夹 (/home/user1) 中,然后尝试将其从网络驱动器复制到已安装的共享 (/home/backups,实际上是 192.168.0.6/backups) , 通过使用 fstab。挂载的共享权限为 777,但所有者和组与运行脚本的不同。我使用 bash 来运行脚本而不是 sh 来解决我过去遇到的另一个“错误替换”错误的问题
The first 2 lines of the file are
文件的前两行是
! /bin/bash
cd /home/vnc/backups
I'm probably not supplying enough information to fully answer this post yet but I can post more information as necessary, but I don't really know where to look next.
我可能还没有提供足够的信息来完全回答这篇文章,但我可以根据需要发布更多信息,但我真的不知道下一步该往哪里看。
回答by Paused until further notice.
The clue is in the error message:
线索在错误消息中:
/bin/sh: cannot create : nonexistent
Notice that it says "sh". The Bourne shell doesn't support some features that are specific to Bash. If you're using Bash features, then you need to tell Bash to run the script.
请注意,它说的是“sh”。Bourne shell 不支持某些特定于 Bash 的功能。如果您正在使用 Bash 功能,那么您需要告诉 Bash 运行该脚本。
Make the first line of your file:
制作文件的第一行:
#!/bin/bash
or in your crontab entry do this:
或在您的 crontab 条目中执行以下操作:
* * * * * /bin/bash scriptname
Without seeing your crontab entry and your script it's hard to be any more specific.
没有看到您的 crontab 条目和您的脚本,很难更具体。
回答by sarnold
Perhaps the first thing you should do in your backups.shis insert a cd /home/user1. crondmay execute your script from a different directory than you think it does, and forcing it to use the same directory regardless of how it is executed could be a good first start.
也许你应该做的第一件事backups.sh是插入一个cd /home/user1. crond可能会从与您想象的不同的目录中执行您的脚本,并且无论它如何执行都强制它使用相同的目录可能是一个好的开始。
Another potentially useful debugging step is to add id > /tmp/id.$$or something like that, so you can see exactly which user account and groups are being used to run your script.
另一个可能有用的调试步骤是添加id > /tmp/id.$$或类似的东西,这样您就可以确切地看到正在使用哪些用户帐户和组来运行您的脚本。
回答by Shusen Yi
In crontab, just change 2>$1 to 2>&1. I've just done one myself. Thank you Dennis Williamson.
在 crontab 中,只需将 2>$1 更改为 2>&1。我自己刚做了一个。谢谢丹尼斯威廉姆森。

