0

From the basic of php i know that php needs to have some action/request to execute so i am little confused about how to do it. I know it can be done but don't know how.

I want to write a php script which will run in server every 6 hours and update the database info from an api.

More Info:

The server i am currently working is in linux. But i want to know how i can do it in both linux and windows.

UPDATE:

Cron does not find my script. I don't know where is the problem is. I have used this command in my cpanel

0 */6 * * * php public_html/path_to_dir/file_to_run.php

I have setup the cron so cPanel send me email. The email i am getting is showing some error.

/bin/sh: 0: command not found

Looking forward to your help.

4
  • 1
    Have you considered using chron jobs for this? Commented May 27, 2011 at 13:03
  • 1
    @Pradeep: cron. Has nothing to do with the Greek chronos :-) Commented May 27, 2011 at 13:05
  • @Quentin @Fabio M. @Bobby Jack: Update the problem. Please check! Commented Jul 10, 2011 at 10:32
  • Did you install php5-cli package? Does the file /usr/bin/php exists? Commented Jul 10, 2011 at 11:11

3 Answers 3

6

You need to have something run the script on a timer. This is typically going to be cron (on UNIX based systems such as Linux, OS X, BSD, etc) or Windows Task Schedular (on Windows).

Sign up to request clarification or add additional context in comments.

3 Comments

This said, the timer doesn't need to run on the server, though. If the PHP script is accessible via the Interwebs, any machine with a cronjob defined can call it, for eaxample via cUrl or wget.
So, i will need two types of scripts for windows and linux? @Boldewyn: Not sure what you mean :( Can you give any links where i can start?
No. One script. You just need to use a different program to run it on a timer. (Although I think you can get cron for Windows)
3

You can use crontab to schedule a process in Unix.

1 Comment

this is the best way to do it - the only thing is you need to make sure your host supports it AND you need to know how to connect to your server using SSH (google the program called Putty)
3

I assume that you're using a Linux based S.O.

  1. Install the php5-cli package as root with apt-get install php5-cli (or your pkg manager).
  2. Write and test your script with the PHP CLI, php filename.php.
  3. Login as selected user and set up a crontab using crontab -e
  4. Write the crontab line: * */6 * * * php /full-path/filename.php

/var/log/messages should log the crontab activities.

2 Comments

Hi, i am using this bit of code for adding the corn job 0 */6 * * * php /full-path/filename.php will it run every 6 hours?
Update the problem. Please check!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.