1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Cron

Discussion in 'HTML, Graphics & Programming' started by toastyman, 28 May 2006.

  1. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    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
     
  2. Augmented

    Soldato

    Joined: 18 Oct 2002

    Posts: 5,464

    Location: London Town

    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.
     
  3. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    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
     
  4. robmiller

    Capodecina

    Joined: 26 Dec 2003

    Posts: 16,522

    Location: London

    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.
     
  5. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    Hmm its on a shared server anyway so its probably not worth it. Cheers anyway and thanks for explaining that one!
     
  6. Beansprout

    Man of Honour

    Joined: 31 Jan 2004

    Posts: 16,316

    Location: Plymouth

    What're you trying to do?
     
  7. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    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...
     
  8. Beansprout

    Man of Honour

    Joined: 31 Jan 2004

    Posts: 16,316

    Location: Plymouth

    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) :)
     
  9. robmiller

    Capodecina

    Joined: 26 Dec 2003

    Posts: 16,522

    Location: London

    You could just create a pseudo-cron system.
     
  10. Beansprout

    Man of Honour

    Joined: 31 Jan 2004

    Posts: 16,316

    Location: Plymouth

    I think that's how vBulletin and the like must do their scheduled tasks etc.
     
  11. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    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: 28 May 2006
  12. Beansprout

    Man of Honour

    Joined: 31 Jan 2004

    Posts: 16,316

    Location: Plymouth

    Oooh clever, SMF does that too :D
     
  13. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    I did something right! :D :D
     
  14. robmiller

    Capodecina

    Joined: 26 Dec 2003

    Posts: 16,522

    Location: London

    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.