How to write PHP cron jobs without cPanel? - php

I know we have cron jobs in PHP. but I have a project in development phase and we won't have cPanel access.
We have a PHP + MSSQL application that needs to check the database periodically every 1 minute and collect the data and send a mail to a store admininstrator.
How can we do this?

You can have a alternative of cron jobs solution by implement you function in a file (eg: /very/secret.php if your jobs need to be secure, make sure the function can be call only when it get the right parameter eg: /very/secret.php?key=long-random).
Then use some free cron job server on the web like: https://www.easycron.com/ or https://www.setcronjob.com/ (just do a web search for "free online cron jobs"). You give them your URL and some configuration and then your jobs will be executed by them at a specific time of day.

I understand that you need to run a script to check the database periodically every one minute and collect the data and send a mail to store admin. Also I understand that you have a script and you need to run that script every one minute.
It can be done using "cron job", if you are using Linux server. Or if you are using Windows, there is a way to schedule the task (to run that script every min).
Note: it is nothing to with cPanel. Actually, the cPanel provides a user friendly GUI to schedule the cron job.
But if you are not using panel, you can do it manually.
If you are using Linux, here you can see, how to add the cron job - http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
If you are using Windows, here you can see, how to add the cron job - http://windows.microsoft.com/en-us/windows/schedule-task#1TC=windows-7

Related

How to automatically call a php script every day to send out automatic emails

Is there a way where I can automatically call a php script after a specified interval.
I have a php script(say remindusers.php) that uses mysql to query a database where people have submitted their weekly reports. This script automatically queries the database and sends an email reminder to people who have not sent in their weekly reports yet.
What I am now supposed to do is give the ADMIN an option to set a reminder start and reminder end date during which calls should be automatically made to my remindusers.php script and cease on reminder end date.
What I learnt from SO/google is that I can setup cron (in LINUX) to automatically call my remindusers.php, but I dont have any shell access to do this.
Else Can I write another php script to essentially sleep every 24 hours and automatically wake up to call my remindusers.php script.
Are there any other built-in methods ?
Any ideas?
Use your site's visitors to trigger the event. Send a message and then check if 1 day elapsed. Then send another. You still need to pay attention not to double/triple/... send deu to synchronization.
When the time has elapsed use a MySQL (or system) MUTEX to ensure only one send occurs.
Yes you can! What you need to do is to use cron jobs. Cron jobs are essentially telling the server to execute a script (PHP or otherwise) at regular intervals. Cron jobs are very powerful and customizable, as you can set virtually any interval for your cron.
If you are using CPanel to manage your site, there is a button in CPanel to view all the cron jobs you have set. There is also a tutorial on that page.
Hope this helps.
Try with this PHPCron
PHPCron is a simple PHP script which lets you run multiple tasks on a schedule or timer. It can be run either from the command-line or via a web browser. Its behaviour is very similar to the popular cron program for UNIX.
http://katyscode.wordpress.com/2006/10/17/phpcron-running-scheduled-tasks-from-php-on-a-web-server/
I understand that you don't have Shell access but have you had a look at the cPanel to see whether there is an option to setup a cron job in there?

How do i keep a php script active even if nobody opened the website?

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.

Is cron job/scheduled task service enabled?

I'm making a website that need's a cron job or a scheduled task to send emails weekly but the problem is that I don't know if the host supports it...
This website is for an institution and I can't put it in any other host...
The host is using Apache.
If it doesn't support, how can I send weekly emails automatically, without changing host?
EDIT
I forgot to say that I'm new in cron jobs.
Apache has nothing to do with cron jobs as the system running on the host must trigger the job (which then can invoke a script running under apache).
Do you have SSH access?
Add the job to the /etc/crontab file or the cron-file of your user.
Or do you have a Webinterface to some management software (e.g. Plesk)?
Search there for an option for Cron jobs or Scheduled tasks.
If not you can use some external services which will call an URL on you site to trigger the job like http://www.setcronjob.com/.
First, ask your host if cron jobs are supported (they should be), check your panel (if there is one), try to set up one and see if it works etc.
If not, one possible way (other than to find an external service that will do the call to the script for you) is to add a function to your code that will be called every time a visitor of your site enters a specific page (e.g. the index). There, you will check if the weekday is the day that you want to send the e-mail. If yes, then send the mail, having a flag (e.g. a record in the db) to check if it has already been sent.
Of course it's not the ideal solution, all the others (the actual crons or an external service) are better, but since it is a solution, it's worth mentioning.
Have you read some documentation about cron-daemon?
This code is check the the cron is running
ps -ef | grep cron
After then you need to create a file which can handle the email address pickups and send th mails.
And also need to add something like this to the cron (it just an example send out something at every sunday, 23 o'clock):
0 23 ? * 0 php /path/sendnewsletter.php

Is there a way in which a PHP or shell script can setup a cron job on a web server?

I am writing a component for Joomla and there is a specific task that requires an update to some stats every so often. I would like to setup a cron job to do this. The only problem is that requires the user to go and setup the cron to run the php update stats script.
On installation of the component how can I automatically setup a cron job for the user? Is this possible?
I've seen this implemented in the Akeeba backup pro component for Joomla, so I was hoping that I would be able to do the same thing.
Thanks
In theory you can create a crontab file and call it from PHP
<?php
exec("crontab $path_to_cron_file");
in practice it depends on wether the server (if you're on a shared hosting) allows you to do that.
All you need to do is write a line to the crontab file, generally stored in /var/spool/cron/crontabs/username. The cron daemon will see that the file modification time has changed and reload it automatically when it wakes up to do its checks
Another option (less desirable from a server load perspective but easier for users) would be to create a plugin that is run each time a visitor visits the site. It could check whether the process has been run in a specified time and then run it if it needs to be run.
iJoobi.com has another solution where they have set up a server to run cron tasks, which would then ping the specific URL on the website to kick off the process. (http://www.ijoobi.com/Help/jNews/jNews-Cron-Task.html)

How to send on a specific day in php?

I want to a e-cards or something like that. The user can choose the e-cards, after chosen, he must enter the some fields like name(to and from), email(to and from), message and I want to let user to choose which date to send the e-cards.
How to send the e-cards on specific day? I need to write a script that run every new day? How to do that? Sorry, I am new to php... (but not beginner like not even know how to execute mysql query, get message from url etc)
Yes, you need a script that runs every day. (Barring ridiculous maneuvers like trying to fake this by checking on Web requests.) The usual way to do this in a Unix context is called a cron job; if your hosting provider is Unix-based, you should look into what they provide for making cron jobs available to you. On Windows there's a parallel service called Scheduled Tasks.
A similar question was disscussed here resetting-a-mysql-field-value-without-user-execution
I'll just reiterate: There are web based cron services too. This could come in handy if you only got a shared hosting plan and can't add cron jobs. They will call an URL at a regular interval that you can set. Usually very cheap. (Cheaper than upgrading to a root-access server anyway.)
Just search Google for web based cron
ciao!
/0
This is for *nix.
Let's say you have a php script that sends email on a specific day called mailer.php
<?php
//mailer.php
if (date("m/d/Y") == "06/02/2009") {
mail("client#email", "Subject", "Body");
}
?>
We are going to assume that you already have cron daemon running in the background.
If you have root access to your machine, then setting up a cron job is simple as editing a file.
Open up /etc/crontab file and add the following task:
1 14 * * * root php /path/to/your/scrip/mailer.php
This means, as a root, the mailer.php script will be running daily at 02:01PM. You can change the numbers to whatever you desire.
This is somehow complex. First it depends on your system. If it is Linux/BSD/Unix/Solaris then you have this handy utility as cron. If you are using Windows, you have Scheduled Tasks. Run your script daily (or as you wish) and check what cards you have to send today.

Categories