how to run a php script daily with the cron job on Ubuntu os

ぐ巨炮叔叔 提交于 2019-12-01 09:47:02

To find out what is wrong with your cron you can type the following command in your terminal:

grep -i "cron1.php" /var/log/syslog

The syslog contains all log of crons.

Try run the code /usr/bin/php5 -q /var/www/cronjobs/cron1.php on terminal to check if there are errors.

You can also redirect all errors to a file:

0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php 2> /tmp/errorCron1.txt

You can use crontab to add/remove/edit cronjobs.

Hit Alt+Ctrl+T to open terminal.

First make sure the script is executable by running:

chmod +x YOURSCRIPT

Then run the following command to add your cronjob:

crontab -e

Add your cronjob like this:

0 * * * * /usr/local/bin/php path/of/php/file

That's it!

Your can check the current user's crontab entries by running:

crontab -l

For more information about crontab run:

crontab --help

OR

man crontab

Make Script File /etc/scripts/cron.sh with contain

cd /var/www/

/usr/bin/php cron.php

Save it then

chmod +x cron.sh

then go on /etc/crontab

15 15 * * * root /etc/scripts/cron.sh

save it and wait

This is the description of crontab arguments

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
0        2          12             *                *            /usr/bin/find

To Run your script every hour use below crontab entry.

0 */1 * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php

This way your script will start executing every hour.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!