I would like to be able to test a server side process in my local dev env using a CRON task that runs every minute. However I do not want this to run every minute of every day, just when I need to test the process.
Is there some Terminal command I can use to add a CRON task and another to do the reverse (remove it).
Ideally I'd then like to execute this command via a tool like Shuttle so that I can start/stop the CRON from the taskbar as and when required.
In the end I abandoned this idea and used an app called CronniX to manually start and stop a local CRON process:
https://code.google.com/p/cronnix/
Related
We have a website running on multiple Azure instances – typically between 2 and 5.
There is a PHP script I would like to schedule to run every few minutes on each instance. (It just makes a local copy of data from a system that couldn't handle the load from all our users hitting it in real-time.)
If it were just one instance, that would be easy - I'd use Azure Scheduler to call www.example.com/my-scheduled-task.php every 5 minutes.
But the script needs to run on each instance, so that every instance has a reasonably up-to-date copy of the data. How would you achieve this? I can't work out if it's something in Azure Scheduler, or if I should be looking at some sort of startup script?
You can use a continuous webjob for that.
Just tweak your php script to have a loop and add a sleep of a few minutes between runs of your code.
The continuous webjob will run on all of your instances and even if somethings fails it will be brought back up.
Per my experience, a PHP webjob running on your each webapp instance is the good solution as #AmitApple said. However, I think you can try to use a scheduled webjob with a CRON expression for ensuring a start time, not a continuous one with a sleep time. And please make sure the script can be completed in the interval time.
You can refer to the section Create a scheduled WebJob using a CRON expression of the doc Run Background tasks with WebJobs to know how to get start.
Please see the note of the section Create a continuously running WebJob https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#CreateContinuous.
Note:
If your web app runs on more than one instance, a continuously running WebJob will run on all of your instances. On-demand and scheduled WebJobs run on a single instance selected for load balancing by Microsoft Azure.
For Continuous WebJobs to run reliably and on all instances, enable the Always On* configuration setting for the web app otherwise they can stop running when the SCM host site has been idle for too long.
I have a cron which runs every minute but the thing is it queues every request from the past minute and executes some tasks one after another. I want to run a background process which will run infinite time. I'll check if there is any new request came in & will process that immediately.
do {
//do my stuff
} while(true)
I need to know the command to check if the process is running or not, if not then start this, else do nothing
FYI - I'm not a linux guy and dont know anything about bash or shell. I need PHP code which I can add in the every minute cron which will just monitor this process is running or not.
What you are looking for is a service control and/or a watchdog. You can use D. J. Bernstein Daemontools or similar software.
Also, if you want to do it in PHP you could, inside the start part of your daemon (that is what you are building) raise a flag (a file), then withing a cron job, run another program to check if the flag is raised (the file exist) every N minutes.
I have a project, i need to do this
a desktop application sends a txt file with a number to the web server every 5 seconds
the web server opens that file and saves the number in a database
the thing is that i need it to work 24/7 , even if the user hasn't logged in.
the desktop application already works, what can I do?
You should use a cron to do this. Here's an article explaining how to set them up in linux:
http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
In case you're running this on windows:
What is the Windows version of cron?
Mac is similar to linux:
http://benr75.com/pages/using_crontab_mac_os_x_unix_linux
use Unix Chron to plan a php job that fits your needs
What you're trying to do is not actually a web server function, per se, (there isn't a request to serve) you just want to run a PHP script on a pre-determined schedule. To do that, you should run a scheduled job (cron on non-windows, Scheduled Task on Windows).
We use Windows, so here's how you set up a scheduled task for a php script to run when you need it to:
On the web server, from Control Panel, create a new Schedule Task
Set the Run value to wherever PHP is installed and the script you want to run:
"C:\Program Files\Php5\php.exe" C:\webserver\scripts\myphpfile.php
Set the Start in to wherever your php file is:
C:\webserver\scripts\
Set up the rest of the Schedule Task options to your needs.
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
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.