Server-Side PHP Task Scheduling

I’m green at php AND DH server admin. My question is:
How would you solve the need to have a time scheduled (market hours in this case) PHP program run on the server? This would need to start without intervention; read a file; do calculations; write a new file. A similar one would send an email with the file attached. It would terminate when finished; and write to a log on start, error, finish.

I am able to do all this when starting the app from a browser, but can’t figure out how to schedule it to happen on a schedule.

If it was my server, perhaps I could run a cron job (if I knew how :slight_smile: ).

Ideas? Guidance? Pointer to documentation on DH?

Pete A

The task scheduler is know as cron.

And this:
Should get you on your way. Also type cron in the wiki search box, there are a number of related articles.

THANKS! Read all that, set up a cron job using the Control Panel.
If you have a second, can you help this non-unix guy to understand what this means? /home//path/to/script.php
I’ve tried everything I can think of for “home” and for “” and for “/path/to/.” Got the script.php part :).

And this: “/usr/local/php5/bin/php” I THINK I can just use php for this…but not clear.

I’ve now tried 20+ different combinations with messages like:
Could not open input file: Ttxxx.Com/
Could not open input file: php/rsicalc1.php
Could not open input file: /usr/local/php/rsicalc1.php
Could not open input file: /php/rsicalc1.php
Could not open input file: rsicalc1.php
Could not open input file: /txxxs/txxxs/php/rsicalc1.php
Could not open input file: //
Could not open input file: /
Could not open input file:

I have no idea where home pathto are, or what they are. UNIX Noobie. It also says can’t run under admin account…that’s the only one I have. Do I have to create a dummy user and move all the php apps under it? He says, walking through the swamp.

A real example (with a few letters changed as I did above) would sure help; can’t find one anywhere!


Pete A

for example, if my SFTP/FTP username is "bob"
and my script is "schedule.php"
in a folder called ""
and I am using php 5.3

the cronjob would be:
/usr/local/php53/bin/php /home/bob/

Adding a little bit to sierracircle’s to help you understand… (and using the same example names sierracircle used.)

When you log in via ftp/sftp etc, (assuming you haven’t set the software to do something besides what is defaulted) you will be in your users home directory or /home/bob/

in that directory you should find a directory named the same as the site you are hosting or . When you change into that directory you are now in /home/bob/

if schedule.php is in that directory the path to schedule.php would be /home/bob/ . If you have extra directories (folders) to get to schedule.php you would just include them … perhaps /home/bob/

Now lets look at /usr/local/php53/bin/php /home/bob/
what we really have there is a command to start an instance of php using its complete path (i.e /usr/local/php53/bin/php) followed by a space and an argument we are passing to that instance of php which is the php script we want to run. There are multiple versions of php on the server, so it’s always best to use the full path to the version you want rather than just php (which I think will still start things off with deprecated version 5.2).

BTW on a completely unrelated topic, since I used the term ftp, I really should say for security you should have your user set to “disallow ftp” and you should always use SFTP.
I also just re-read post 3 of this thread.

A website should never run under an admin user. You should only use an admin user when you need the elevated privileges.

Instead of trying to find out what /home/username/ is, use the $HOME environment variable. That’s what it’s there for (including changing to your $HOME directory when the cron job starts).

/usr/local/php54/bin/php $HOME/php/rsicalc1.php

You can test this from the command prompt.

This below doesn’t answer the question directly, but it’s a good approach.

An even better future-proof way it to use a shell script, then you don’t have to change the cron job. This would always be called from cron as:
/bin/sh $HOME/

“You can’t trust Internet quotes.” – Abraham Lincoln

#!/bin/sh - Run the php script with cron, or command line

DIR="$HOME/php/" # might be changed later
CMD=rsicalc1.php # could soon be version 2

DATE=date +%F # YYYY-MM-DD
NICE="$(which nice)"

PHP = /usr/local/php/php54/bin/php # for now use PHP 5.4

PHP = /usr/local/php/php55/bin/php # waiting for 5.5 testing

log stderr and stdout from the PHP script

$NICE $PHP “$HOME/$DIR/$CMD” 2>&1 “$LOG/$CMD”_"$DATE".log

will email when ran by cron, since this script is outputting

echo $HOME/$CMD completed.