无法从运行crontab bash脚本时,它的命令行工作的bash [英] Cannot run bash script from crontab when it works from command line bash

查看:318
本文介绍了无法从运行crontab bash脚本时,它的命令行工作的bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有其事能够从从命令行运行bash脚本而不是从根crontab条目中一个奇怪的问题。我运行Ubuntu 12.04。

I have a strange problem of being to able to run a bash script from commandline but not from the crontab entry for root. I am running Ubuntu 12.04.

* * * * 1-5 root /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log

如果我运行使用bash CMD线的脚本,它工作正常,但 SH 失败,以下错误:

If I run the script from the cmd line using bash, it works fine but sh fails with following error:

> jmeter-cron-randomise.sh: 7: jmeter-cron-randomise.sh: arithmetic
> expression: expecting primary: "  % 1 "

已经一派的问题,这似乎是标准的shell不具有相同的数学运算符,如%(模数),作为庆典。我不知道为什么cron作业在脚本失败?我假设这是因为它没有使用bash shell?这肯定是由cron守护程序(可以看到它在/ var / log / syslog的)发射。任何帮助非常AP preciated。

Having googled the problem, it seems like standard shell doesn't have the same math operators, like % (modulus), as bash. I'm Not sure why the cron job is failing in the script? I am assuming it is because it's not using the bash shell? It's definitely being fired by the cron daemon (can see it in /var/log/syslog). Any help much appreciated.

推荐答案

您可能需要告诉的cron,要使用shell是bash shell的,因为它默认为SH。你可以通过把这个线在crontab做所有的crontab条目:

You likely need to tell cron that the shell to use is the bash shell as it defaults to sh. You can do that for all crontab entries by putting this line in your crontab:

SHELL=/bin/bash

请注意,这将导致在crontab所有脚本到bash下你想要什么了不得运行。如果你想改变的crontab行本身只是运行bash的,改成这样:

Note that this will cause all scripts in the crontab to be run under bash which may not be what you want. If you want to change the crontab line itself to just run bash, change it to this:

* * * * 1-5 root /bin/bash /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log 2>&1

请注意,我还造成标准错误被写入cron.log文件(2>&安培; 1)这可能不是你想要的,但pretty常见的做法。这可能会帮助您进一步从脚本诊断错误。

Note that I have also caused stderr to be written to the cron.log file (2>&1) which may not be what you want but is pretty common practice. This may help you further diagnose errors from the script.

这篇关于无法从运行crontab bash脚本时,它的命令行工作的bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆