0

I have an website with mobile phone prices and all prices are collected from external siites manually, all I want is to schedule update prices ($emag, $koyos)every 3 days.

My manuall update query is this:

$editare = "UPDATE modele SET koyos='$koyos', emag='$emag' WHERE id ='{$id}' ";

if ($dbh->query($editare))
 {
   print "<div><h2><img src=\"http://mysite.com/images/ok.png\"/><br /><br /Succes!</h2><br />
  Vei fi redirectionat in cateva momente inspre adaugarea unui nou model de tableta sau telefon!
<meta http-equiv=\"refresh\" content=\"2; URL=index.php\"/>";
}
 else mysql_error(); 

How can I do this query automatic at 3 days?

! My webhost not support cronjob SSH, cPanel or Plesk !

4
  • I am not verry familiar with cron jobs Yogesh Commented Mar 21, 2013 at 9:39
  • Its a scheduling task, only you have to configure your server to execute to run the script every 3 day, the script will be your current code. Commented Mar 21, 2013 at 9:42
  • See my answer here: stackoverflow.com/questions/14916507/nodejs-for-cron-jobs/… Commented Mar 21, 2013 at 9:54
  • @lesus Good idea on the image, I added this to my answer below. Commented Mar 21, 2013 at 10:00

1 Answer 1

4

Firstly, you need to write a script in PHP that does what you want to do (basically, it's just your PHP code that you want to run at the scheduled time, no additions needed).

Then, using a cron job, you schedule this to run as often as you want.

*/4 * * * * wget --spider file.php in crontab runs every 4 minutes. You might want to read up on some crontab tutorials.

Here's an image from one of the links below describing how to schedule for the time you want:

enter image description here


Cronjobs are basically schedules that run every so often. They're really useful, and I use them all the time on Linux.

The link above is really useful for explaining what numbers you need to type to execute a script every x number of minutes, but you may need to google "Cronjob calculator" to automatically calculate the right numbers for you (I do this myself sometimes).

In linux, with crontab installed, type crontab -l to see a list of all cronjobs. Type crontab -e to edit your cronjob list for the current user. This is where you paste the code I gave you above.

Edit: To get your head around cronjobs at a basic level, first install crontab. Then, use the code above to execute a script that writes to a file. Then, just tail -f that file, and watch how (in four minutes after the cronjob starts) it updates in front of your eyes.

Start with the basics. Then get it to execute your MySQL script.

Useful Links:

http://www.linuxweblog.com/crotab-tutorial
http://clickmojo.com/code/cron-tutorial.html (this looks good)
http://www.tutorial5.com/content/view/95/51/
http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/ (minutes / days explanation)

Note: You can't do less than a minute. The last link suggests a sleep() command to get around this, so check that out.


The problem with OP's question is that because he uses very restricted shared hosting, he doesn't have access to SSH, cPanel, cronjobs or anything else like that.

An option here is to spin off a separate process for wgetting the PHP file using exec(), or shell_exec(), with nohup and an ampersand (&) at the end, along with a long sleep() time for the number of days the OP wants. This will cause the PHP script to run in the background on the server.

Safeguards need to be put in place with the above example, ie: if the server is rebooted then the script will no longer be running and will have to be restarted. Conversely, if the user accidentally hits the script at the wrong time (or someone else does) then multiple processes will be spawned, so a safeguard to check whether the process is already running for example could occur (or a basic password entry requirement may help to avoid accidental hits).

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

9 Comments

Thanks alot, you alightme... now I am try
Where must be putted */4 * * * * wget --spider file.php command? In my site's script or in phpmyadmin or must be placed in other place? Thanks!
I have an shared webhost and there is no SSH or CRONJOB
Cool, you should be good then. You're looking for "how to schedule cronjobs using cPanel". Should be plenty of google search results. I was worried you didn't have CPanel for a while then! Maybe this link may help?
Is there any option than cronjob ssh, cpanel or plesk? these 3 are not suported by my webhost.
|

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.