Setting a 1and1 Cron Job

The 1and1 Cron job facility is pretty simple really but not very rewarding on initial testing. It is the testing that seems to throw people, like me off the scent. So this page provides a snap shot of one way how to set a Cron job on 1and1 shared hosting. There may be other, better ways but this worked for me.

My most crucial messages to you are;

If you think you are right with your code, then leave it overnight and do something else…the servers take time to refresh.

If you’re not sure, then write ten different commands, in ten different styles, then leave it overnight and do something else……the servers take time to refresh.

Crontabs (cron jobs) are Chronicled jobs which operate behind the scenes, executing scheduled tasks via the server. In this example I’ll show you how to set a cron job that opens a php file which searches a database and then emails the result.

Creating a Cron Job in 1and1 shared web hosting

The task is possible, cron jobs are acheivable through OneandOne shared web hosting packages of ‘Business Pro’ and ‘Professional’. It took me about a day-and-a-half of heartache getting there but eventually I managed to get the server to deliver the scheduled task.

What I wanted to do was automate a database search and to post the results through email, using php as the processing script language.

How I did it.I will assume that you already know how to create a database through OneandOne, and already know how to use php to access that database to do your search. What you really need to know is how to set up the cron job itself. I’ll start with the solution and work backwards from here.

The Solution in a Nutshell. The solution is to use the SSH command line through a facility like ‘Putty’ to get to your root directory. The 1and1 help files are very good at providing you your access codes (basically you need your domain name: www.yourdomain.co.uk, username: u3692**** and password: p*****rd).

See the image below for what your Putty command line screen looks like. The image below is a listing (crontab -l) of two scheduled tasks which take place on the server at 1:58am and 1:12am every day. There are two lines on this example, simpy because I found two different ways of doing the same task and wanted to share them with you, however any individual approach will work. Plus probably a few more that I don’t know about yet.

The magic key to setting up these cron jobs turns out to be providing the correct path to ‘wget’ or ‘lynx’, with oneandone it happens to be the /kunden/usr/bin/wget path and /kunden/usr/bin/lynx path that you never get to find out about. The rest is simply pointing to the file that you want to be executed, in this case a php file that opens a database connection, runs a sql script on a table and returns the result as an email back to me.

You may need to wait a while…

The biggest problem I had however is that I arrived at the above solution(s) after only a few hours of messing around with Google and crontab, but the solution didn’t seem to work. So I tried a miriad of different approaches only to keep coming back to the above examples.

Until finally I decided to walk away from my frustrations with my crontab code. I left it on the server (in desperation) while I slept on it and hey presto the next day. There were fresh emails in my inbox. It was working! I really don’t know why it took all-night to work but it hasn’t stopped working since and my server carries out it’s functions every night at the same time without error or hesitation.

My advice is… have the courage of your convictions and let the 1and1 shared hosting server catch up with your crontab command.

crontab -e & crontab -l

To enter or edit a crontab you need to enter crontab -e <return>, from here you need to press ‘i’ to insert or edit the current set of crontab commands.

To save the crontab you have just entered or edited don’t forget to press Esc-Shift-Z-Z rather like you would press Ctrl-Alt-Delete on a PC. Otherwise your cron job will not save.

To simply list the current set of cronjobs you simply type crontab -l <enter> at the command line.

Setting the Cronjob timing

The easiest bit is setting the cron job timings, there are pages and pages of this on line so I won’t bore you, just google it. The hardest bit is getting the cronjob to work in the first place.

Good luck and if you are having difficulty I have left the discussion open for you to share your experiences or grumble at your frustrations.