PHP Cron job windows and linux - php

I need to set up cron/automatic taks on windows(shared)/linux(shared)/wamp servers.
the problem is that the project is running on multiple environments.
so what is the best way to set up cron/scheduled taks ?
Actually what i need to do is check email servers for new emails and if something found save it to the local DB. If you have any alternative other than cron job then please let me know.
Thanks.
EDIT:
As i mentioned in the question i have multiple emails/filtering , so i need to run something on background to fetch data periodically. CRON in linux and Scheduled Tasks on Windows.
But the real problem is that i am doing it on a shared hosting ( or it depends the client ) so i cannot use CRON/Scheduled Tasks.
Ex : Project is installed on GOdaddy windows shared hosting , it is a windows server so no support for CRON(normally) and they wont allow to use Scheduled Tasks.
the question is : is there any alternative for CRON/Scheduled Tasks
?

Short Answer
I don't know alternatives to CRON/ScheduledTask which would fulfill your need.
I suggest you outsource the schedule to another server, see my possibilities below.
I came up with the following possibilities:
Shared hosting with CRON jobs (easiest)
Look for a shared hosting provider who lets you add cron jobs (e.g. through webspace management). HostEurope (german) would be such a host.
You own a dedicated (virtual) server
Given you deploy this project to multiple shared servers but own a dedicated (virtual) server for yourself:
Make your script publicly available but guard it with a strong authorization mechansims. (hard-to-guess request token, white-list certain IPs as callees, ...).
Set up a cron job on your own server which calls the script on the client's webhost.
You don't own a server
As the last possibility but you don't own a dedicated server.
Setup a virtual machine at some cloud provider (e.g. OpenShift) and add the cronjob there. Don't use this instance for other jobs and it should fullfill your needs perfectly (Reference: https://openshift.redhat.com/community/blogs/getting-started-with-cron-jobs-on-openshift)
Requirements don't meet infrastructure (likely!)
Your client/project has requirements which don't fit a shared hosting environment. You are strongly encouraged to get a hosting plan fulfilling your true needs. Price differences between shared hosting and first virtual servers or dedicated hosting aren't so steep that investigating is out of question.

As everyone else suggested if this is a web page you can use wget to run it.
If it is a CLI script you will have to run it with php /directory/filepath.php
If the actual question is HOW you are going to periodically run it you will have to use
cron on *NIX and scheduled tasks on windows server.
If you want to automatically install the cron you will have to check the OS and act depending on whether the OS is windows or *NIX.
A google search will give you results on how to do it in both environments.
Edit after reds clarification
As Samuel Herzog pretty nicely says, on shared hosting you (usually) have a control panel.
Most well known for linux are:
Cpanel: http://www.siteground.com/tutorials/cpanel/cron_jobs.htm
Plesk: http://www.hosting.com/support/plesk/crontab
Webmin: Setting up a cron job with Webmin
And for windows I only have some familiatiry with plesk for which the procedure is the same as before.
If you dont have control panel but have shell access (linux) you could follow this tutorial.
If you dont have control panel but have remote desktop (windows) you could follow this tutorial.
If you dont have any of the above you should follow Samuel Herzog suggestion about a vm on a cloud provider or consider upgrading to a VPS or a dedicated server.

Related

How to deploy a php web app behind firewall?

Does anyone know a solution for deploying a PHP webapp behind a firewall on mainly Windows servers? We have 100+ customers who host our webapp on premise, and we would like to setup a deployer, as a part of our bitbucket pipeline, so our code gets deployed on all installations.
1 customer = 1 installation aka deployment
Today we use a small PHP script, and some version control software, to pull code changes once every day. It runs on both Linux and Windows servers.
Hit me with any solutions :)
You can make use of PHPDeployer.
You can setup SSH-access on the servers and then configure the script to deploy to the desired IP of the server.

Establish websocket connection on a shared hosting?

I'm working on a php websocket chat app and everything's working fine, but the problem is I went to a hosting company to get a hosting plan and after I've uploaded my app websocket didn't worked because I didn't find a way to run server.php file.
On my local machine, I was able to run server.php file from the terminal.
What I wanna know that, Is there any way to create a php file act like server.php?
You need server hosting with access to shell/bash console. This is usually unavailable on shared ones. The basic hosting that would allow you to run this is a VPS or advanced a dedicated one.
But with VPS you might run into problems with hosting company if your script takes too much resources or the VPS instance is configured so that it will kill 'lingering' processes (depends on VM and configuration).

How to run exe file on web server use php

I have hostgator cpanel web server. And I want to run exe file on it with php. Is it possible to do that? On window computer it is possible to start a program with the help of php command exec() with command line arguments. I'm looking for a solution so that I can do the same on my web server too.
The first thing you must understand is the difference between a web host and a dedicated server. On a dedicated server, you do what you want whereas a shared hosting server is shared among hundreds, sometimes thousands of users.
That difference is reflected in the price and justifies severe restrictions that need to be imposed by the host to maintain a good overall service level.
That said, functions of the exec() and system() family are also available for Linux, but you won't be able to run Windows executables on Linux without additional tools such as Wine which certainly won't be available on a shared host.
If your hosting sever running UNIX-like OS, Linux,
for example, it's not possible, you have to look for alternative binaries on that server.

need to exec() ffmpeg on shared hosting, 1and1 hosting

and I've read a bunch of related posts here on SO and have gotten the following ideas here:
First, my website is shared-hosted at 1and1.com. And my objective is to call exec() in my PHP code to run ffmpeg to convert short videos (30 seconds) from format A to format B.
1) Now, because of my shared hosting situation (the 1and1 web server my website runs on is shared with other 1and1 customers) -- 1and1's administrator probably has their server in 'safe' mode, so that my desire to exec() in PHP to do the ffmpeg conversion of a video -- will fail.
(Supposedly there is a 'safe mode' shared directory but that's a hoop to jump through with 1and1).
2) And even if 1and1 does not mind if, from my PHP code, I call 'exec( ffmpeg -i yada yada)' for converting between video formats -- I've heard it's unseemly to do that. I'm led to believe I need a dedicated server and not a shared server. Which sounds weird because (a) the web site will never scale to massive # of users, and (b) when I run ffmpeg on my 2 1/2 year-old laptop, Windows Vista, running Xampp as my localhost development web server, the conversion happens like right now. Very fast.
Are the above restrictions correct ('safe mode' on shared web hosting, running ffmpeg is a no-no without a dedicated box)?
I'd like to think that 1and1 would not even notice if my web site did a few (say, under 20) short-video conversions per day.
Also if anyone can recommend the proper web hosting company for my situation I would be in your debt. Thanks.
I believe safe mode would allow you to use exec because 1and1 would have you chrooted so any program you run would not be able to access other users. But, you might want to check the AUP to see if the prevent those kind of processes (that consume a lot of resources).
For the best performance you should use a VPS server in which you can control your php environment. ffmpeg has potential to be very resource intensive, so for best performance a VPS/dedicated would be best. Also, using ffmpeg on shared hosting would impact performance on other sites as well.
Any VPS would do, but because you asked for a recommendation, I would recommend BlueMileCloud.
PS: You should never call a process directly from exec. You should add it to a queue, so the script does not wait for the process to finish and cause the site to hang.

PHP Sockets or Python, Perl, Bash Sockets?

I'm trying to implement a socket server that will run in most shared PHP hosting.
The requirements are that the Socket server can be installed, started and stopped from PHP automatically without the user doing anything. It doesn't matter what language the socket server is written in, as long as it will run on the majority of shared hosting globally.
Currently, I've written a Socket Server with PHP that implements an Object Cache:
http://code.google.com/p/php-object-cache/
source: http://code.google.com/p/php-object-cache/source/browse/trunk/socket.class.php
However, PHP has to be compiled with sockets support, and not many servers run with PHP sockets support.
My real question is: What language should I implement the socket server in, and have maximum platform support and be invokable from within PHP.
In other words, what scripting language is the most common on PHP enabled Servers?
Or do I have to write the socket server in a compiled language to have it works across all servers?
Lets leave IIS out of the picture at the moment, just Linux servers. I don't think many PHP sites are running on IIS...
edit:
Sorry I think my question is not clear.
I'd like to know, what languages is best suited for creating a socket server given the following requirements:
The language must exist in shared hosting, alongside PHP running in Apache (not CLI).
The sockets support must be enabled natively, not via a required extension.
PHP must be able to write the deamon to file as well as start and stop the deamon.
I'm not asking for a solution for a single server. It has to run natively on the majority of shared hosting servers.
Any server can be stopped or started by PHP under Linux. Of course, if you are running a server which accepts sockets from the internet, then you can just connect directly to the server and tell it to shutdown. No need to go via PHP!
As for "starting a server from PHP", well, under Linux, anything can be started from pretty much anything. Just shell out to start the process and have it drop into daemon mode.
I'm a Perl fan myself. Not surprisingly, there's a
Perl Daemon library available.
If your hosting provider offers Perl script support, then you probably have permission to use "system" or backticks command. Then you can very likely start a daemon. However, you will need to use a non-privileged port (over 1024). Also, you should ASK THEM FIRST! They may not appreciate you tying up ports on their server. This is very definitely something you should discuss with your hosting provider.
It really depends on what the install requirements are. Often the easiest and most standard way to write a socket server is to write an inet.d service. This is a standard daemon on my unix machines, and it will fork a process and handle the socket level details. If you want your service to run on port below 1024 on Unix, this is one of the easier ways to get it done. However, the initial install requires root to configure inet.d.
If you shared hosting allows PHP to do an exec call, then you could start the daemon that way. Keep in mind though, it'll need to run above port 1024. You next need to decide if your program is going to be multi-threaded or multi-process. Typically Java programs are multi-threaded, while an Apache instance is normally multi-process.
Lastly, the host may have a firewall in place. This helps prevent shared hosting accounts from becoming part of a bot-net. If the firewall rules don't allow connections to other ports, you won't be able to connect to it remotely.

Categories