Jump to content

using exec() to run scripts that update a DB


spectator

Recommended Posts

hey guys,

i'm a php newbie, but i've worked with c++ and java most of my life. i am working on a web project now and ran into this problem:

 

i am using a cron job to update a db every night. the php file that is used, has several (4) other php files that are executed sequentially in this form:  exec("<filename>", $output); print_r($output);

 

here is the problem; if i run the 4 scripts from command line (on the server), they execute perfectly; DB is updated correctly and all is well in the world.

 

before i get any further, here's what the 4 scripts do:

1. download some text files

2. update db with that info

3. remove expired info from db

4. delete text files from server

 

when i run the one file (let's call it "run_all") both from the command line and cron job (i'm saying this b/c i tested the cron jobs, and it's not the issue), i get the following output:

 

Array

(

  [0] => X-Powered-By: PHP/5.2.12

  [1] => Content-type: text/html

  [2] =>

)

 

Finished executing download script

 

2. Started updating the DB

Array

(

  [0] => X-Powered-By: PHP/5.2.12

  [1] => Content-type: text/html

  [2] =>

  [3] => Opened file

  [4] => Opened database

  [5] => An error has occurred.  No item(s) added.

  [6] => File closed successfully!

)

 

Finished updating the DB

 

3. Started removing expired info from DB

Array

(

  [0] => X-Powered-By: PHP/5.2.12

  [1] => Content-type: text/html

  [2] =>

  [3] => Error: Could not connect to database.  Please try again later.

)

 

Finished removing expired info from DB

 

4. Started deleting text files from local directory

Array

(

  [0] => X-Powered-By: PHP/5.2.12

  [1] => Content-type: text/html

  [2] =>

)

 

Finished deleting text files from local directory

 

 

i guess that one way to solve this(maybe) is to make the 4 files run at 5 minute intervals in 4 different cron jobs

however, i would still like to do just one file; i would appreciate any suggestions; i could be doing something that is taboo and i just have not learned it yet.

 

cheers

 

 

If timing is important:

we can try storing task number and time.

 

So, we done task 1, set task = 2 and time when task 1 was done, so set time = 13:10.

Then when time is 13:15, check if 13:15 - 13:15 = 00:05, then if difference between them is 00:05 or more do next task which is 2, and increase task by 1, and set new time

once it gets to task 4, change task to 1.

 

If time not important:

if you have cron job every five minutes we just store task number.

here is example of procedure:

task = 1..

do task 1

task = task + 1

 

then next time cron runs file,

 

task = 2..

do task 2

task = task + 1

 

for storage we can use database or file.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.