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:

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

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

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 Nice priorities 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 ?

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

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

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

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

THANKS EVERYONE FOR THE INPUT @sm4rk0 answer solves my problems


Both methods should work, but IMO it's cleaner to put nice into the script just as you did in your example. After a check with man nice I found that example in question had wrong syntax. The right syntax would be:

nice -n 10 command

Nicenesses range from -20 (most favorable scheduling) to 19 (least favorable).

And, as terdon said, 10 is the default value so "-n 10" can be omitted.

Note: Although it will lower the process priority, nice won't make it cause less CPU load. So, if only reason to worry about is not to "choke" the apache, then this method is OK. But if your hosts give you some CPU time quota, this won't help a lot.

  • The tech support team repeatedly blocked it after the script ran for about 10 minutes and slowed down the server. They don't seem to have CPU quota or I am well under that considering that my website itself is not resource intensive. – xlordvader Jan 17 '13 at 3:13
  • Is that command correct on the syntax ? I have never tried Nice nor nice with a cron in a script. And I don't want to get kicked of the server again. Pain to talk with tech – xlordvader Jan 17 '13 at 3:24
  • Yes. The nice syntax is OK. Give it a try, then come back here to update us if it worked. – sm4rk0 Jan 17 '13 at 3:27
  • I'm sorry, the syntax was not OK. Correct syntax would be: nice -n 10 command – sm4rk0 Jan 17 '13 at 4:14
  • Actually, just nice command is enough, it will run the command with a priority of 10 by default. – terdon Jan 17 '13 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.