I have a script that will update some columns on my database. It is written in PHP, I execute it via URL (eg. http://foo.com/xyz/yzx/dbupt8r). I did this using crontab -e then curl on the script URL, because on my mind it is like somehow similar of what I am doing: accessing it via URL. Is there any advisable or better way of doing this? Am I at security or threat flaws?
I have a script that will update some columns on my database. It is
Share
There are two ways to do this, the way that you’re already doing it: (
curling a publicly accessible URL); or executing the PHP script directly from your crontab.Cron Curling
As you mentioned, this is often very convenient and comfortable since you’re already developing a web application in PHP and so it’s the way you’re already working. There are a few risks:
curl: It also means you’re relying oncurlto execute your script, so you’re opening yourself up to many points of failure (curlitself, DNS, etc.).Running PHP from Cron
Alternatively, you may be able to run the script directly from your crontab. There are two ways of doing this:
Passing the PHP script to the PHP interpreter binary, which would look something like this (note the path to your PHP binary varies by platform, but should be specified as an absolute path as cron doesn’t have access to many environment variables):
Alternatively, you can add a hashbang/shebang line to the first line of the PHP script as follows:
Make it executable, for example:
And add it directly to your crontab:
The advantages of this method are that you can put the script in a location that’s not publicly accessible so you can ensure tighter control over its access & execution. It may also mean you can write lighter code if you don’t have to handle the web side of things. That said, if you’re using a PHP framework, you may find it difficult to develop a stand-alone script such as this.