PHP Cron

Associate
Joined
30 Dec 2005
Posts
415
Ok here's an interesting one...

Is it possible for a PHP to setup a cron job?
Is it possible for a PHP to detect a cron job and read its settings?

Cheers
 
I don't see why not - all it involves is reading and writing to the correct cron file e.g /etc/crontab or /var/spool/cron/username, which PHP has plenty of filesystem functions for.

Main issue I can see is limited permissions that the PHP user will normally have that will prevent it from being able to write to the relevant file. Probably best to set up a cron for the PHP user and read/write to that.
 
Ok that's good news I suppose! I'm surprised there isn't actually a PHP function to create and edit cron jobs...

The bad news is I don't know where to start :p
 
There isn't an existing function to do this simply because there is no way to get it working on all server set-ups. In short, most people run CGI scripts as the user "nobody" or "apache" or somesuch, a user which (for security reasons) rarely has access to anything except the files within the web directory. As such, modifying crontabs - which are well outside the web directory - is impossible.

If you run as your own user you might have a little more luck, but I wouldn't guarantee anything.
 
robmiller said:
There isn't an existing function to do this simply because there is no way to get it working on all server set-ups. In short, most people run CGI scripts as the user "nobody" or "apache" or somesuch, a user which (for security reasons) rarely has access to anything except the files within the web directory. As such, modifying crontabs - which are well outside the web directory - is impossible.

If you run as your own user you might have a little more luck, but I wouldn't guarantee anything.

Hmm its on a shared server anyway so its probably not worth it. Cheers anyway and thanks for explaining that one!
 
I'm creating an install script for my system...yes you have to copy all the files to the server, but it sets all the chmod automatically, registers with the master site, creates the database tables, and I wanted it to set up a cron job as well...

My alternative is that if you can't use PHP to set up the cron job, could it see whether the cron job has been set up via crontab or ssh, before it lets you continue with the installation...
 
I'd advise that you ask the user to set it up manually :)

Also - make sure you have a failsafe for the chmod because as robmiller said on some setups the webserver doesn't run as the actual owner of the files, so you'll have some problems there (or potential insecurities in forcing the user to chown the files to the webserver user) :)
 
Beansprout said:
Make sure you have a failsafe for the chmod because as robmiller said on some setups the webserver doesn't run as the actual owner of the files, so you'll have some problems there (or potential insecurities in forcing the user to chown the files to the webserver user) :)
I read about that, so I set the CHMOD permissions using FTP. It seems to work on all 3 servers i've been testing on.

I will probably use psuedo-cron...it looks good enough for what I need.

Also, I don't know if you remember a few weeks back we had a discussion on encrypting PHP files...I know its not secure, but this seems like an adequate deterrant: http://www.byterun.com/free-php-encoder.php
I was just thinking of encrypting the API page which talks to the master site..
 
Last edited:
How is chmod'ing via FTP any more secure? If your PHP scripts run as nobody there is absolutely no way to have your scripts be able to access the files but not have other server users' PHP scripts access the files, whether you CHMOD with FTP or not.
 
Back
Top Bottom