How to set CRON to execute a php file outside of a public html folder?

15
2014-01
  • Richard Rodriguez

    I have a php file located at, let's say, for example:

    /data/web/process.php
    

    This file is not publicly available (no virtual host is running in that folder, but apache is installed). I need to execute this file every minute every day.

    I know I can open crontab with crontab -e, but I'm not sure what exactly should I write to the file so that what I need to do works.

    Any help, please?

  • Answers
  • Michał Šrajer

    run crontab -e and add line:

    * * * * * /usr/bin/php -f /data/web/process.php 2>&1 >> /path/to/output/file.log
    

    Before you do that, make sure that /usr/bin/php -f /data/web/process.php does what you want. Also note, that cron will run it as same user who called crontab -e, so if you run it as user rimmer it will run as rimmer, if as a root, it will run as a root.

    if you want it to be executed as apache user, do:

    sudo -u www-data crontab -e
    

    assuming www-data is your apache user. You can also create a file /etc/cron.d/my-php-job.

    Make sure no bad guys can edit /data/web/process.php file and review that file carefully. I would make this file root owned and with 644 permission. Do not make it apache user owned and writable.

  • Justin Pearce

    Try the following:

    /path/to/php -f /data/web/process.php
    


  • Related Question

    unix - How to create a cron job to upload files to an FTP server
  • Christopher

    I would like to create a cron job that uploads files from a directory on my computer to my FTP server. I would like it to do it daily at midnight. I know pretty much nothing about cron, so I apologize if I sound stupid!


  • Related Answers
  • Alan Shutko

    This is an FTP sample script to transfer one file: (Note you can use an FQDN instead of IP)

    #!/bin/bash
    
    # $1 is the file name for the you want to tranfer
    # usage: this_script  <filename>
    IP_address="xx.xxx.xx.xx"
    username="remote_ftp_username"
    domain = sample.domain.ftp
    password= password
    
    ftp -n > ftp_$$.log <<EOF
     verbose
     open $IP_address
     USER $username $password
     put $1
     bye
    EOF
    

    Add the > ftp_$$.log only if you need logging. Then you can use the

    crontab -e
    

    command to edit the cronjob table and add your script.

    This is an Example:

    If you liked to have a the script above, (assume you have it in home and its name is myscript.sh) /home/myscript.sh, run every day at 2am, you have to do:

    # crontab -e
    

    and then you have to add the following entry:

    0 2 * * * /home/myscript.sh
    

    As a reference, here you have a crontab entry parameters meaning:

    * * * * * command to be executed
    - - - - -
    | | | | |
    | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
    | | | ------- Month (1 - 12)
    | | --------- Day of month (1 - 31)
    | ----------- Hour (0 - 23)
    ------------- Minute (0 - 59)
    

    This tutorial could also help you.

  • Brian Agnew

    man crontab will show you what you need. You'll want something like:

    0 0 * * *  yourScript.sh
    

    in your crontab file. Note that scripts under cron run with a cut-down environment, so you'll have to specify your env settings that the script requires in that script.

  • Yasir Arsanukaev

    man 1 ftp says that -u URL file [...]:

    Upload files on the command line to URL where URL is one of the ftp URL types as supported by auto-fetch (with an optional target filename for single file uploads), and file is one or more local files to be uploaded.

    Thus something like

    0 0 * * * /usr/bin/ftp -u "ftp://example.com/dir" file1 file2
    

    would upload files file1 and file2 to directory dir at example.com