如何通过 Cron 运行 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37574458/
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
How to run a bash script via Cron
提问by André Goldstein
I have seen other questions that are similar but I can't find any real information on how to figure out the proper way to run a Bash script via Crontab. The .sh
file is located in the user directory (in my case serverpilot
).
我看过其他类似的问题,但我找不到任何关于如何找出通过 Crontab 运行 Bash 脚本的正确方法的真实信息。该.sh
文件位于用户目录中(在我的情况下serverpilot
)。
The script is trying to copy the contents of the apps
folder and send to my S3 bucket. The script works perfectly when run with sh backupS3.sh
from the terminal but no joy via Cron. I've seen people reference PATH variables and the like but no idea where to even begin!
该脚本试图复制apps
文件夹的内容并发送到我的 S3 存储桶。该脚本在sh backupS3.sh
从终端运行时完美运行,但通过 Cron 没有任何乐趣。我见过人们引用 PATH 变量等,但不知道从哪里开始!
/backupS3.sh
/backupS3.sh
#!/bin/sh
echo 'Started backing to S3'
date +'%a %b %e %H:%M:%S %Z %Y'
aws s3 sync /apps s3://bucketname
date +'%a %b %e %H:%M:%S %Z %Y'
echo 'Finished backing to S3'
crontab
定时任务表
*/10 * * * * /backupS3.sh >> backupS3.log
Can anyone point me at anything obvious I'm doing wrong? Thanks!
任何人都可以指出我做错了什么吗?谢谢!
EDIT: I've added 2>&1 on to the end of the cron command and now I am getting something in the log file:
编辑:我在 cron 命令的末尾添加了 2>&1 ,现在我在日志文件中得到了一些东西:
/bin/sh: 1: /backupS3.sh: not found
/bin/sh: 1: /backupS3.sh: not found
回答by Mark Reed
In pathnames, the leading /
doesn't mean "home directory"; it always refers to the root of the file system, no matter who you're logged in as. You need to use the actual full, absolute path to the script (e.g. /home/serverpilot/backupS3.sh
). If the crontab belongs to the same user whose home directory holds the script, you can use "$HOME"/backupS3.sh
and the system will fill in their home directory path for you. Or, even more simply, just use ./backupS3.sh
, since cron jobs start with their working directory equal to their owner's home directory.
在路径名中,前导/
并不意味着“主目录”;无论您以谁的身份登录,它始终指的是文件系统的根目录。您需要使用脚本的实际完整绝对路径(例如/home/serverpilot/backupS3.sh
)。如果 crontab 与主目录保存脚本的用户相同,您可以使用"$HOME"/backupS3.sh
,系统会为您填写他们的主目录路径。或者,更简单地说,只需使用./backupS3.sh
,因为 cron 作业的工作目录等于其所有者的主目录。
If that doesn't work, then either what you mean by "user directory" is not the same as the POSIX concept of "home directory", or the script is not executable (which you can fix by running the command chmod +x backupS3.sh
once).
如果这不起作用,那么“用户目录”的含义与“主目录”的 POSIX 概念不同,或者脚本不可执行(您可以通过运行chmod +x backupS3.sh
一次命令来修复)。
If you're not sure what the full path is, just run the pwd
command while you're in the same directory that holds the script, and put the output, followed by a slash, in front of the script name.
如果您不确定完整路径是什么,只需pwd
在保存脚本的同一目录中运行该命令,然后将输出放在脚本名称前面,后跟一个斜杠。
回答by SaintHax
First, change #!/bin/sh
to #!/bin/bash
, next use the full path to the script (also .sh isn't needed in the file name)
首先,更改#!/bin/sh
为#!/bin/bash
,然后使用脚本的完整路径(文件名中也不需要 .sh)
*/10 * * * * /home/users/me/backupS3.sh >> /home/users/me/backupS3.log
回答by foobar
I've had the same problem where log file was created empty and the only way to see what is going on was to add 2>&1
at the end of the cron job.
我遇到了同样的问题,其中日志文件创建为空,查看正在发生的事情的唯一方法是2>&1
在 cron 作业结束时添加。
*/10 * * * * /home/users/me/backupS3.sh >> /home/users/me/backupS3.log 2>&1
*/10 * * * * /home/users/me/backupS3.sh >> /home/users/me/backupS3.log 2>&1
If anybody wonders what 2>&1
actually mean here is a short explanation:
如果有人想知道2>&1
这里的实际含义是一个简短的解释:
2 - refers to a file descriptor used to identify
stderr
1 - refers to a file descriptor used to identify
stdout
2 - 指用于标识的文件描述符
stderr
1 - 指用于标识的文件描述符
stdout
So 2>&1
basically translates to: Redirect stderr
tostdout
所以2>&1
基本上翻译为:重定向stderr
到stdout