PHP Daemon Script - php

I was wondering how to make a php daemon script that runs one time at the day?
Do you know any good frameworks with benefits?
or is it just small code?
Thanks

I was wondering how to make a php deamon script that runs one time at
the day?
In order to do this, get familiar with cron jobs. A cron job is a function that gets executed by the server on a time interval. Usually you'd edit your "crontab" by executing crontab -e
Then, once inside, you'd write the interval you want, followed by the command.
Typically it looks like:
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
Since its PHP, you can either a) run your php command as a php cli command, OR b) you can make the command get executed when a particular page is run... and just execute that in cron via a curl -X GET 'http://url/' (etc.)
Also, note that you can write all of your stuff in a shell script file and actually run that file as your cron command... that reduces line-item complexity

cron

Sorry I haven't closed this one.
I actually discovered that my host didn't allowed cron jobs running. So I found a relevant homepage that offer a free service to make a request for me when I needed. In my case, I have specified a url link that should be requested to my RESTful API each day.
The link is here and works like a charm :)

Related

How to create and start an infinite background script in php?

I want to create a PHP script that will run non stop and execute some instructions every minute/hour (according to my need). It should never die.
How should I proceed to this one? How do I start the script itself?
What Iv'e done so far is created an infinite for loop and checking the time and if it's a new minute then calling my function. But when I call the link, it shows my browser is busy.
I want the script to run on Google Compute Engine without any need to call the URL from a second computer. I want something like android foreground service but for PHP.
Cron job is the answer.
Cron is basically a job scheduling daemon. It runs in the background and is executed automatically. You can set it to run at any time that you wish to. This nature of cron makes it useful for automating tasks.
A cron job, is a task which we carry out with the help of a cron.
So if you run a php script with the help of a cron, it'll run on the background. We don't need to run any infinite loops in the script. You can also set a cron job to run at specific time. How it is run and when it is run is totally upto how you configure the cron job. For that you have to edit the crontab file. A cron tab file is a text file containing a list of commands meant to be run at specified times and the commands in the crontab file along with their run times are checked by the cron daemon, which executes them in the system background.
Now, to create or edit entries in your own cron tab file:
$ crontab -e
Add an entry in the crontab file. One thing you have to remember is, for executing php scripts, use the php executable and call the php script from your crontab as shown in the example below
*/2 * * * * /usr/bin/php /var/www/html/project/cron-file.php
This reference might be helpful to you.
Hope this answer helps.
For Unix-system, best practice for such tasks is using Cron: https://en.wikipedia.org/wiki/Cron
Not only for PHP, but any periodical tasks.

How to run a cron job without using exec or system function in PHP?

Gist
I am trying to run a cron job in PHP. But the exec and system functions didn't work. (Because the server administrator doesn't allow me to use these two functions due to the security reasons) However, I still need to use PHP to run it, because the running time is decided by the formula and it's uncertain. As a result, how can I run the cron job without using exec or system function in PHP?
My thought
The only way I have come up with is adding the cron job via the cPanel, which is set to “once per minute.” That means setup a cron job for every minute to check the target PHP page whether it has anything to do at that moment.
Issue
There's a possible issue if it always checks the page every minute per day, won't it damage the host? (Maybe it will cause a damage to CPU or maybe occupy the memory space, etc.) If so, how to improve it?
You can call a bash script containing a call to PHP-CLI and then the file:
#!/usr/bin/php
/path/to/my/php/file.php
Go to the command line of the server and type "which php". Replace /usr/bin/php above with that which is returned. Then you call this bash script in your CRON.
As you say you have a cpanel server, im guessing its a linux box. You run a cronjob from a linux box by typing crontab -e and then to run a php script. */5 * * * * /usr/bin/php /path/to/file.php to un it every 5 minutes.

How to run the PHP script at scheduled time

I need to run a php script at the scheduled time daily to update some fields in database and to send automated email. How I can do this?
Is it possible to write some service in XAMP server to run the script daily at scheduled time? I have no idea how to update database and send email automatically at the scheduled time. Can any one share some ideas or concepts?
I am using PHP and MySQL running in Linux Server.
You should use a Cron job to do it. Check out the examples on the Wikipedia page.
The Cron Job should call a script using the php executable that runs the necessary task.
Just create the script that does the required job, test it by hitting the URL in your browser once you are sure it works right. Copy the URL and add a Cronjob
Then schedule this command to run at whatever time you want to run
php ABSOLUTE_URL_TO_SCRIPT >> logfile
The log file is optional. But it will give you a chance to see what happened.
For example if you want to run your script every 4 hours, and assuming your script is at http://localhost/work/scripty.php and assuming that your http root is /var/www,
you would run "crontab -e" in terminal and add the following line:
* */4 * * * php /var/www/work/scripty.php
If you need more information just comment I will update the answer.
PHP cannot run script by itself,since php doesn't have daemons like python !!
So you have to take OS help to invoke your custom script .
For example in linux :
(example.sh)
export USE_PHP=php
cd $SCRIPT_ROOTDIR
$USE_PHP -f cronfile.php service="checkdatabase"
(service is the parameter passed to your cronfile).
For setting up cron jobs ,have a look at this link
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
In Linux, We can create .sh file and can give a specific time to run that is called cron job.
SO should use this method just make a shell file and give a time period to it.
You should take a help with linux expert for that.
Use the following: Cron Job

How to schedule in php

I have some files on my server, how to open them programatically once a day?
Let them be
http://site.com/scripts/video.php
http://site.com/scripts/music.php
Without my hands, just like sheduling (automatically).
Even if I sleep and server is working, they should open on given time.
And additionally, how to open them once a 10 seconds (for tests)?
Thanks.
The Solution is very clear when you are using a Linux server;CRON JOBS.
One can easily run a cron job by configuring it through the terminal.I saw everyone has provided the Solution,but my answer will be for the people who are novice to Linux servers and don't know much about Cron Jobs.Go to Terminal and type the below commands..
root>which php
The above line will give you the path to where PHP is in your linux systems
Now,
root>crontab e
The above line will open the Cron file in edit mode.
Enter the number of times you want to run a particular php file and what time of the day,month,week,etc.
I am providing the syntex for running a particular file every 15 mins.
So here you go,
(write this in the cron file in edit mode)
*/15 * * * * path/to/your/php path/to/the/file/you/want/to/run
Now,path/to/your/php has to be replaced by the path what you got when you typed
root>which php
And you are done just save the file and close it.You will see a messege on you terminal that a new CronJob is installed.
That's it.
If you're on a Linux/Unix host using a cron job is generally the best approach, as you can simply call the command line version of PHP as a part of the cron job. (You may need to tweak your script if it relies on $_SERVER variables, that said.)
Administration middleware (such as Plesk) often offer the ability to add cron tasks as well, although you many need to check the user/group rights that such tasks are executed with.
Finally, if you use a cron task you can simply enter the required command via the command line during the testing phase. (i.e.: Rather than force a 10 second update (which would be tricky unless you had cron execute a shell script) you could execute the script as required.)
It's not possible with pure PHP. You'll need a cron job for this - ask your provider or administrator whether they are available.
Cron has a resolution of 1 minute, though: Calling a script once every 10 seconds would have to be done e.g. using a PHP script that gets called every minute, and makes six requests every ten seconds.
Running them once a day requires a seperate program running them.
For linux servers the usual choice is a Cron Job, for Windows the Task Sheduler works fine, too.

How to make PHP code work without direct calls (on some kind of timer)?

So... for example I want to add to 1 five every 5 minuts (1 is in the DB)... With out direct calls from users....
So... How to make PHP code work without direct calls (on some kind of timer)?
If you are unable to schedule cron jobs on your server (as is the case with most cheap hosting solutions), there are some pure php alternatives to run scheduled jobs: phpjobscheduler is one of those alternatives.
build a script which does what you need to do. and call it via crontab in the needed interval
but make sure its not callable from a user or searchengine.
see http://www.unixgeeks.org/security/newbie/unix/cron-1.html for more information on cron
a typical call could look like:
*/5 * * * * lynx -source http://yourhost.com/yourscript.php >/dev/null
Yes configuring a cron job is the correct answer.
See the Wikipedia article for the syntax of a cron job.
You simply create a new cron job and let it request the page where the script is.
The following cron job requests update.php every five minutes.
*/5 * * * * wget http://www.example.com/update.php
Update
Syntax with wget.
If I understood you correctly, CRON is what you're looking for (google it)
Cron is the obvious suggestion.
If you can only invoke your code via the web (i.e. no command line PHP) then you can call a URL using 'wget' or 'curl' on most *nix boxes.
Unless of course your code sits on a microsoft OS (which is unlikely to have cron available) in which case you can use the 'at' service to do the same thing - and there's a free wget.exe out there somewhere.
C.

Categories