Running a PHP script with a Cron job in 1and1
Following on from running my first cron job with 1and1 hosting what I really wanted to do was to be able to run a php script, perl script or some other form of script on a timed basis. This article is a description of setting up a cron job on 1and1 hosting to run a simple "hello world" style example. I have the 1&1 linux business hosting package so can only confirm this example works with that.
First I set up my helloworld.php script, this was a simple script to create a file called helloworld.txt and write the words "hello world" to that file and save it.
<?php
// the filename
$filename = 'helloworld.txt';// the content
$content = 'hello world';// create the file
$f=fopen($filename, "wb");
fputs($f, $content);
fclose($f);// now done
?>
I tested this file by running it on my server and it worked fine. I would recommend you do the same.
The next thing I had to do was log on to my 1and1 server using SSH, the program I use for this is PuTTY which I have been using since university and works well. You enter the username and password for FTP and SSH access to your site in the PuTTY command prompt and then are logged into the site. The next two steps you have to go through are:
- to find the location of your current directory: enter "pwd" to find this... mine was: "/kunden/homepages/XX/dXXXXXXXX/htdocs"
- to find the location of php on your machine to do this I entered the command "php -info" in the command prompt and looked for the location in the output which was: "/usr/local/bin/php" however due to the nature of 1and1 hosting the full path was actually "/kunden/usr/local/bin/php"
Now I had to create my crontab file. In the file below the 14 refers to the 14minutes past the hour, you can find out about the 4 *'s from the crontab page on wikipedia. So I entered the command "crontab -e" (which means edit crontab) and came to the VI editor which 1and1 use on their debian linux installation. In that line I entered the command: "14 * * * * /kunden/usr/local/bin/php /kunden/homepages/XX/dXXXXXXXX/htdocs/helloworld.php > /dev/null" all on one line. This command means at 14minutes past every hour on every day using the program found at /kunden/usr/local/bin/php execute the file found at /kunden/homepages/XX/dXXXXXXXX/htdocs/helloworld.php and send the output to the location /dev/null (which means get rid of the output). Using this command I found that on 1and1 hosting my helloworld.txt file was updated between 14 and 18minutes past every hour.
I know this description would have helped me had it been available when I was looking for information on running a php file through a cron job on 1and1. I hope it helps you.

Thanks so much! This was extremely helpful.
Posted by: Jeff | April 29, 2008 at 12:45 PM
Huge thanx for your article! 1and1's FAQ is useless, does not give real examples. You saved me some hair on my head ;)
Posted by: ILJA Tirins | October 13, 2007 at 01:21 PM
*sigh*. After finding your "My First Cron Job with 1and1 hosting" I looked for your follow up to PHP but couldn't find it. I just spent about 2 hours and FINALLY got it working, and then I clicked on your page and found all the answers I had just discovered sitting for me here.
Great post... wish I had found it sooner (the only thing I now understand [as opposed to before] is the > /dev/null)
Thanks,
Kerry
Posted by: Kerry | September 07, 2007 at 05:52 PM
Hi Alex,
Where'd you find "/kunden/usr/local/bin/php"?
I did a "whereis php" in PuTTY, and got this:
php: /usr/local/bin/php
/usr/local/lib/php3.ini
/usr/local/lib/php5.old
/usr/local/lib/php
/usr/local/lib/php.ini
/usr/local/lib/php.ini-nourl
How'd you figure out it needed the "/kunden"?
Thanks,
Dan
Posted by: Dan Kelly | August 07, 2007 at 01:53 PM
Thanks for this article! It was really useful and saved me a lot of heartache in trying to figure out how to find how 1and1 accesses the php program.
Posted by: Paul Borrego | May 24, 2007 at 10:05 AM