yhi Posted August 24, 2016 Share Posted August 24, 2016 i want to make a script which will read a php statement execute that statement & then remove from the file for example suppose file task.txt contain all php statement to be executed task.txt: statement1 statement1 statement1 statement1 statement1 statement2 statement2 statement2...............now the task of script is to execute first file of "task.txt" & after execution remove it from "task.txt"after one time executiontask.txt: statement1 statement1 statement1 statement1 statement2 statement2 statement2............... after second timetask.txt: statement1 statement1 statement1 statement2 statement2 statement2...............after third timetask.txt: statement1 statement1 statement2 statement2 statement2...............i hope you get it and please note that i want script to run only when cron job call itit means it execute & remove statement after every cron job call Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/ Share on other sites More sharing options...
Jacques1 Posted August 24, 2016 Share Posted August 24, 2016 (edited) Don't do that. Seriously, don't. Fumbling with text files is already a bad idea (we have SQL databases for that). But putting actual PHP code into that file and blindly executing it is so misguided and insecure that I wonder how you even got this idea. Tell us what you want to achieve, not how you think you will achieve it. What do those statements do? I'm fairly sure they are predefined actions, in which case you can use queues in a database. Edited August 24, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536564 Share on other sites More sharing options...
yhi Posted August 25, 2016 Author Share Posted August 25, 2016 i want to execute some statement.. sometimes same statement multiple time.. statement fetch data from another website... so i coded a script for it.. but now the problem is my script is trying to fetch data continuously (one statement then another) & i dont want that.. because it may can flood that website.. & also i dint get reply for my request..so i was thinking to use cron job i will code a script which will store all statement in a text file & another script will execute those command one by one & remove them from text file after execution..i know i can use sleep(); but then my script is taking too much time.. & also sometimes it gives 500 internal errorhope you understand what m trying to say Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536637 Share on other sites More sharing options...
Jacques1 Posted August 25, 2016 Share Posted August 25, 2016 Sure, but that doesn't mean you should put code into some text file and evaluate it. Fetching data from a URL is a clearly defined task, so your script only needs to know the parameters (the URL, maybe additional headers etc.). For example, you could implement a queue in your database, i. e. a table with one row per task. Whenever the cron script runs, it fetches the parameters of the next task, makes the HTTP request and then marks the task as completed (if the request was successful). However, this means you have to manually add the tasks, which may not be practical. Alternatively, you could simply create a list of URLs and iterate over that list over and over again. Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536639 Share on other sites More sharing options...
benanamen Posted August 25, 2016 Share Posted August 25, 2016 (edited) What exactly is the overall thing/purpose you are doing with this "program"?. (I dont mean how you think whatever should be done) Edited August 25, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536640 Share on other sites More sharing options...
yhi Posted August 25, 2016 Author Share Posted August 25, 2016 @Jacques1 to be honest i dont know much about database & mysql.. so i think i will leave a lot of bugs & leave my script vulnerable for attacks..& i know about queue concept i learn about it in c++ Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536644 Share on other sites More sharing options...
yhi Posted August 25, 2016 Author Share Posted August 25, 2016 its a projecti have some RGB leds..to change their color i have to send a request like this IP/RGB/hex_codeIP is local IPRGB is just a folder& hex_code is hex color code of color i want my arduino program will show that color light for 5 second and then turn of if no further command is given.. Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536645 Share on other sites More sharing options...
Jacques1 Posted August 25, 2016 Share Posted August 25, 2016 Please don't duplicate your topics, and try to pick a sensible forum for your questions. I've removed your new thread from the third-party section, because you're already discussing it here, and it's not a third-party script. Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536647 Share on other sites More sharing options...
yhi Posted August 26, 2016 Author Share Posted August 26, 2016 actually that thread was about fastest way to send requestanyway its ok & i will be more careful next time can you help me with my problem ? Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536706 Share on other sites More sharing options...
Jacques1 Posted August 26, 2016 Share Posted August 26, 2016 We might be able to help you if you would be so kind as to state your real, actual problem. No more plot twists. You initially asked about how to make a cronjob execute PHP statements in a text file. Then it turned out you want to “fetch data from a website”. Now it turns out what you actually want to do is control LEDs which just happen to be attached to an HTTP server. So this is about LEDs, yes? And you want to do what with the LEDs? Go through a single sequence of colors? Then the entire cronjob stuff is nonsense. Timeouts can be fixed by disabling the time limit. Anyway, forget about PHP and just give us a coherent description of the ultimate goal together with concrete examples. Something like: “I want to show red for 5 seconds, then green for 15 seconds, then again red for 10 seconds.” Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536708 Share on other sites More sharing options...
yhi Posted August 26, 2016 Author Share Posted August 26, 2016 (edited) i dont want to program the sequence i want user to give that chance i want a user like my mom or a guest to select what sequence they want..& i know its a bit confusing because m not able to explain properly.. anyway forgot everything i said i will try to explain againfor changing the LED color we have to send a http request.. which look like IP/directory/heX_code now instead of changing color manually i want to make it semi automatic.... i want any user to select a color sequence that want like red blue yellow green white & that sequence will get stored somewhere& i thought about setting up a cron job for picking the top color (in our case its red) sent a http request for red & remove red from list & after some interval (which i set for cron job) it pick next color (blue) send http request for it & then remove it..PS i will not actually send red or blue i will store their hex code & send them Edited August 26, 2016 by yhi Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536717 Share on other sites More sharing options...
Jacques1 Posted August 26, 2016 Share Posted August 26, 2016 OK. None of this requires a cronjob. The timeout issues you had earlier can be fixed by simply disabling the time limit. PHP scripts run as long as you want them to run. You don't need to store the sequence either. If the user selects the colors and immediately starts the sequence, the data can simply be read from memory. So this boils down to a simple loop: <?php const BASE_URL = 'http://127.0.0.1/directory'; const SLEEP_AMOUNT = 5; // disable time limit set_time_limit(0); // example colors; this data is submitted by the user $colors = [ '009688', '66CCFF', 'FF4E50', ]; foreach ($colors as $color) { http_request('GET', BASE_URL.'/'.$color); // use cURL to make the request sleep(SLEEP_AMOUNT); } Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536721 Share on other sites More sharing options...
yhi Posted August 26, 2016 Author Share Posted August 26, 2016 bro u r the best let me try itif i get success i will comment here & u can mark this as answered Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536725 Share on other sites More sharing options...
yhi Posted August 26, 2016 Author Share Posted August 26, 2016 if u dont mind i am still curious what will be the fastest way of sending that http request ? Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536726 Share on other sites More sharing options...
Jacques1 Posted August 26, 2016 Share Posted August 26, 2016 The standard library for sending HTTP requests is cURL. Depending on the PHP settings, you may also be able to pass a URL to functions like fopen(). But this is more of a hack and often deactivated for security reasons. Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536728 Share on other sites More sharing options...
yhi Posted August 26, 2016 Author Share Posted August 26, 2016 i added "set_time_limit(0);"still facing "500 Internal Server Error" Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536729 Share on other sites More sharing options...
Jacques1 Posted August 26, 2016 Share Posted August 26, 2016 Open the PHP error log, post the error here. Quote Link to comment https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536730 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.