4

command to runI am using ubuntu 12 and lamp server . I want to run a php script after every 1 hour . i have create a crontab to execute this and if i check my cron list with command crontab -l it is showing like this

# Edit this file to introduce tasks to be run by cron.
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php

# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

this is my php script

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

but it is not executing

how can i check why it is not working , please help

1
  • sorry but it is not working for me
    – user4506671
    Commented Jan 30, 2015 at 12:06

4 Answers 4

10

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
5
  • i am using the same . Do you know how can i run this from terminal just for a check like this ?? "sudo 0 * * * * /usr/local/bin/php path/of/php/file"
    – user4506671
    Commented Jan 30, 2015 at 12:15
  • No you can not run as you want. Did you check that it is running in browser and give desired result? Commented Jan 30, 2015 at 12:18
  • yes it is running from browser. is there a way to run it from command line??? it is also working if i use command "php5 -q /var/www/cronjobs/cron1.php" from command line
    – user4506671
    Commented Jan 30, 2015 at 12:20
  • it is working i i run this "php5 -q /var/www/cronjobs/cron1.php"
    – user4506671
    Commented Jan 30, 2015 at 12:22
  • I think you have to use this for run php script: 0 * * * * /usr/local/bin/php php5 path/of/php/file also you can see extra from here: askubuntu.com/questions/417122/… Commented Jan 30, 2015 at 12:26
7

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
4

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

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.