bash Cron 作业的 .sh 脚本中的好命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14371576/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:16:26  来源:igfitidea点击:

Nice command in .sh script for Cron Jobs

phpapachebashcronnice

提问by xlordvader

I have this php script that I need to run on shared webhosting. I have created a cron job that executes an sh script. The command for the cron was:

我有这个需要在共享虚拟主机上运行的 php 脚本。我创建了一个执行 sh 脚本的 cron 作业。cron 的命令是:

/bin/sh /home/user/script.sh

/bin/sh /home/user/script.sh

So I'm assuming it is Bourne Shell (or something compatible). The script itself was:

所以我假设它是 Bourne Shell(或兼容的东西)。脚本本身是:

#!/bin/sh
cd /home/user/public_html/folder/
#updating DB
php -q ./run_interactive_job.php batch_control_files/updateDB
echo Updated DB results

My question is:
Can I add Nicepriorities to the php command ? Or do I need to add it to the script at the cron command. Which one is more likely to work ?

我的问题是
我可以在 php 命令中添加Nice优先级吗?或者我是否需要在 cron 命令中将其添加到脚本中。哪一个更有可能工作?

nice 10 php -q ./run_interactive_job.php batch_control_files/updateDB

nice 10 php -q ./run_interactive_job.php batch_control_files/updateDB

Would that be successful at running at a lower priority.

以较低的优先级运行是否会成功。

PS:Basically, this script has overloaded the server before when I ran it through the browser and it affected apache on that server resulting in my hosts blocking the file. I have repeatedly asked them unblock to test it with different parameters. And Now I'm trying to run it through cron at a lower priority in the hopes that it won't affect apache. But I don't want it to create issues again, hence I'm trying to use NICE

PS:基本上,这个脚本在我通过浏览器运行它之前已经使服务器过载,它影响了该服务器上的 apache,导致我的主机阻止了该文件。我一再要求他们解锁以使用不同的参数对其进行测试。现在我试图以较低的优先级通过 cron 运行它,希望它不会影响 apache。但我不希望它再次产生问题,因此我正在尝试使用NICE

If anyone has any other suggestion that would offer a similar solution of running the php script without affecting apache or the webserver, that'sgreat too.

如果有人有任何其他建议可以提供类似的解决方案,即在不影响 apache 或网络服务器的情况下运行 php 脚本,那也很棒。

回答by xlordvader

skip the shell script and use

跳过shell脚本并使用

* * * * * /usr/bin/nice -n 10 /path/php -q /path/script.php

nice and\or php path may or may not be required

可能需要也可能不需要很好的和/或 php 路径

回答by xlordvader

I'm sorry I asked this question in two places as I thought I wasn't getting a response. I asked at Superuser and I received an answer that suits my needs.

很抱歉我在两个地方问了这个问题,因为我以为我没有得到回应。我在 Superuser 处询问,我收到了适合我需求的答案。

@Dagon - The answer from Dagon would seem to work as well and I will provide an update when I have resolved this issue with either solutions.

@Dagon - Dagon 的答案似乎也有效,当我使用任一解决方案解决此问题后,我将提供更新。

The same Question asked at Superuser - https://superuser.com/questions/537509 ...

超级用户提出的相同问题 - https://superuser.com/questions/537509 ...

UPDATE: I used the batch file as shown there. I used the nice command before php inside the batch file. The only difference being:

更新:我使用了批处理文件,如图所示。我在批处理文件中的 php 之前使用了 nice 命令。唯一的区别是:

nice -n 10 php -q ./run_interactive_job.php batch_control_files/updateDB

nice -n 10 php -q ./run_interactive_job.php batch_control_files/updateDB

I added more commands to run additional php scripts with the same nice property to them. They all worked great.

我添加了更多命令来运行其他具有相同属性的 php 脚本。他们都工作得很好。

THANKS EVERYONE FOR THE INPUT

感谢大家的投入

回答by tripleee

Either way is fine. If you put the nicein the Cron job itself, it will affect the priority of the entire job; but since the script only contains one command whose priority it really makes sense to set, doing it inside the script works just as well in practice.

无论哪种方式都很好。如果把 放在niceCron 作业本身,会影响整个作业的优先级;但由于脚本只包含一个命令,它的优先级设置确实有意义,因此在脚本中执行它在实践中同样有效。