Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute script on server by user action

Tags:

php

mysql

I didn't really know how to describe my problem on the title so I did my best.

I have a function that reset my website. I also have a time stamp in which that function should execute. the time stamp is changing every reset so I need to find a way to activate that function on que.

My best soultion so far was to write time_to_reset() function and if it's true run the script, but the script is heavy and I'm afraid it won't be possible to run it on some random user (the reset is happening only once) computer. He could exit the website before the script executed completly. Also, I affraid in some situations the script will execute twice or more due of a few users logging in togather. this is another problem I'll be happy to get help solve.

I'm sorry for my bad english, and for the none code question :) I hate it too...

I'll gladly explain again if you didn't understand me. Thank you!

EDIT: I don't have to use the users, it was only an idea I had. I have an Admin panel written in PHP code, in which you can edit the date of the time for reset. There is a way to change CJ dates with php code? or any other way to make the reset happen without using the users?

like image 408
OfirH Avatar asked Jul 18 '15 17:07

OfirH


2 Answers

Queueing actions to run with page loads is pretty easy. Just make a table that holds the action(script/function) and when it needs to be run. Then you can grab the next action and if it's time to execute, just delete the row, that way your script can run and no-one else will trigger the action. If you just have the one action, you can just have the table store only the time to run it at.

In response to your concern that the script is heavy, scripts that are run at set intervals are really what cron was invented for, and for heavier actions on a PHP site, it's really the best option. If you're using a VPS you already have cron, just SSH into your server and run crontab -e to edit your crontab file and put in something like:

*/5 * * * * php /path/to/script.php

to run your script every 5 minutes. (Here's more cron examples: http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/)

Alternatively, if you can't setup cron jobs on your server, you can make your reset action run on its own page and use a webcron service like http://www.mywebcron.com/ to call that page at set intervals.

like image 135
Daniel Nielson Avatar answered Sep 27 '22 17:09

Daniel Nielson


You already know what the problem is.

He could exit the website before the script executed completely

Two solutions for this problem. Basically you don't want to depend on browser for execution.

  • Use Cron Job
  • ignore_user_abort(true); http://php.net/manual/en/function.ignore-user-abort.php. It is very commonly used for taking huge backups.

Also, I affraid in some situations the script will execute twice or more due of a few users logging in togather

Put some locking system i.e don't execute the script if its already running, decent way would be to create a database entry or just a file just after starting the script. So next time your function is executed you can check the status if its already running.


Update as per conversation in comments

You can touch the file say script.lock when the script is executed first time. And also before executing the script check if the file_exists and check modified time with stat

$stat = stat('script.lock'); 
$mtime = $stat['mtime'];

Allow the script to execute only if file is not present and certain time has passed.

Delete the file when execution is completed.

like image 25
Jigar Avatar answered Sep 27 '22 18:09

Jigar