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.