Jump to content

Recommended Posts

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 execution
task.txt:

 

 

statement1

statement1

statement1

statement1

statement2

statement2

statement2...............

 

 

after second time

task.txt:

 

statement1

statement1

statement1

statement2

statement2

statement2...............

after third time

task.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 it
it means it execute & remove statement after every cron job call :)

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/301983-cron-jobs/
Share on other sites

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 by Jacques1
Link to comment
https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536564
Share on other sites

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 error

hope you understand what m trying to say

Link to comment
https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536637
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536639
Share on other sites

its a project
i have some RGB leds..
to change their color i have to send a request like this

IP/RGB/hex_code

IP is local IP
RGB 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..



 

Link to comment
https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536645
Share on other sites

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.”

Link to comment
https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536708
Share on other sites

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 again

for 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 by yhi
Link to comment
https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536717
Share on other sites

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);
}
Link to comment
https://forums.phpfreaks.com/topic/301983-cron-jobs/#findComment-1536721
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.