Nice commands in a .sh script for cron jobs
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.
Related videos on Youtube
xlordvader
Updated on September 18, 2022Comments
-
xlordvader over 1 year
I have a php script that I need to run on shared webhosting. I have created a cron job that executes an sh script. The command in my crontab is:
/bin/sh /home/user/script.sh
I'm assuming it is Bourne Shell (or something compatible). The script itself is:
#!/bin/sh cd /home/user/public_html/folder/ #updating DB php -q ./run_interactive_job.php batch_control_files/updateDB echo Updated DB results
I have following questions:
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 the above command 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 for unblock it to test it with different parameters. And now I'm trying to run it through cron at a lower priority - hoping 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. All of them worked fine.
THANKS EVERYONE FOR THE INPUT @sm4rk0 answer solves my problems
-
xlordvader over 11 yearsThe 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 over 11 yearsIs 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
-
sm4rk0 over 11 yearsYes. The
nice
syntax is OK. Give it a try, then come back here to update us if it worked. -
sm4rk0 over 11 yearsI'm sorry, the syntax was not OK. Correct syntax would be:
nice -n 10 command
-
terdon over 11 yearsActually, just
nice command
is enough, it will run the command with a priority of 10 by default. -
xlordvader over 11 years
nice -n 10 php -q ./run_interactive_job.php batch_control_files/updateDB
worked just perfectly. I ran multiple php files one after another with no hickups. Checked with the tech and they said the server was fine as well.