0

I'm doing a server running Ubuntu Server 12.04 and Apache. The users have continuous updates in the dinamic pages through a Java script, downloading JSON files through Ajax request to server. All the users need the same data provided by a PHP file from a secondary mySQL database. To reduce the server load I decided to update the JSON file each second and save them in the server providing them as static content (like a txt file). For this reason I need a solution to run a PHP or any other language script on the server every second.

For future necessity i can't implement a logic like: compile the JSON at the first request and keep them saved for the other requests in the same second.

I read about the CronTab in Ubuntu but it dont allow me to perform the code each second. I tought about changing the entire Cron code 'cause I can't find other solutions.

Someone know if there is any solution in Ubuntu or maybe in the Apache codes to do it? I need something stable and reliable. Someone ever heard about a sistem to perform code on server without the necessity of requests; to manipulate data each second?

Any suggestion is welcome even complex but for a reliable result!

1

I have no idea what you have said in the first two paragraphs, so I can't tell if there's a better solution (my instincts scream there should be).

To run a command at X-second intervals, there are many options, discussed in this Stack Overflow question and this Unix & Linux question.


Execution of command a second after the previous command finishes

Simply put, use a script like this:

#! /bin/bash
while sleep 1
do
   # your commands
done

Execution of command a second after the previous command starts

Same as above, but you send the command to the background. This will result in overlapping command executions if the command takes longer than 1 second to complete.

#! /bin/bash
while sleep 1
do
   (# your commands )&
done

Now, to make sure that the script is running, use an Upstart job:

description "a good description"
start on runlevel [!016]
stop on runlevel [016]

respawn

exec /path/to/the/above/script

Save this is to a .conf file in /etc/init, such as /etc/init/some-job.conf. Now you can start the script with:

service some-job start

The respawn option will make Upstart restart the script if it ever exits (it shouldn't).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.