My First Cron Job with 1and1 hosting
A cron job is a way in Linux of getting a command to run on a schedule of your choosing. That command can be a script or a basic Linux command string such as copying a file. For me being able to execute cron jobs on my server was a key gap in my arsenal of web development skills that has led to me building all kinds of strange hacks to run timed jobs.
This weekend I set myself 3 tasks: 1. start my green card application, 2. find somewhere to start taking GMAT lessons and 3. learn how to do a cron job. In order to start this I needed a cheap host which provides linux hosting with cron jobs and the obvious choice seemed to be 1and1 hosting. I have spent much of today reading how terrible 1and1 hosting is and beating myself around the head with trying to get a cron job working on 1and1 shared hosting. Here is my first very simple cron job with 1and1 shared hosting:
Firstly you must SSH into your 1and1 hosting, I like to use PuTTY to do this.
You then have to log in to the SSH once connected using your FTP username and password from 1and1. My first cronjob was to create a copy of a file inside the directory structure, I uploaded the file helloworld.php to my directory using FTP and then copied the file helloworld.php to the file hello.php using the command "cp helloworld.php hello.php" in my SSH connection. To check this had worked I then ran the command "ls -l hello.php" and found that the file had just been copied by looking at the date and time of last update.
Having confirmed I could run these commands myself I wanted to create a cron job to execute the command. To edit my cron job I needed to create a crontab. The command to open and edit a crontab is "crontab -e" and I entered this in my SSH connection and saw the following screen:
This is the vi editor which is the editor 1and1 uses on debian linux. I personally was trained on emacs and so it was a bit tricky getting used to this editor. The first thing to do is hold down the shift key and hit "I" on your keyboard which accesses the INSERT mode on vi editor. Then you need to enter your Cron Job code. Mine looked like this:
45 * * * * cp helloworld.php hello2.php
The "45" stands for 45 minutes past the hour and the following *'s mean first hour (0-23) then day of month (0-31) then month of year (0-12) and finally day of week (0-7) (but I decided not to specify any of those).
The final thing to do is press "esc" and then hold down the shift key and press "Z" twice which saves your job and exits the vi editor. You should then get a message which says "crontab: installing new crontab" and if nothing goes wrong that's all you will see. The cron job will then run at 45minutes past the next hour assuming everything works smoothly.
If you want to see how close to that time you are you can always type "date" into the command prompt and hit return.
Fingers crossed I will be able to do something cleverer with my crontab soon involving scripts and when I can I will share that here because I am suffering extreme frustration finding information on and working out how to do anything with scripts and crontabs on 1and1.


Awesome! After searching the web for hours on how to install a cron job with 1&1 hosting I came across your page. I finally managed to do it thanks to your tutorial! Good job!
Posted by: Robby | August 09, 2008 at 06:37 AM
hi all,
although this thread is a year+ old. it has helped me set up my cron job. i do have a question. how do i make a cron job for this:
"give autoresponder a maximum of 15 minutes to send message to member."
i tried putting it in like this:
/15 * * * *.....etc.
but i keep getting an error. so right now i have the cronjob like this:
15 * * * *...etc.
(which i know runs every 15 minutes.
anybody?
thanx in advance,
alx
Posted by: alx | March 22, 2008 at 11:14 AM
I have 1and1 hosting as well.. and I can't get the cronjobs to work at all. I tried contacting their support, and they wont' help me. just keep linking me to the FAQ. which isn't helpful at all. I understand the commands and can install a crontab but it's just not working.
example:
u40796793:~ > crontab -e
crontab: installing new crontab
u40796793:~ > date
Fri Feb 29 06:26:59 EST 2008
u40796793:~ > crontab -l
# test cron
28 * * * * date +%H%M > $HOME/test.txt
u40796793:~ > date
Fri Feb 29 06:28:09 EST 2008
u40796793:~ > cat test.txt
cat: test.txt: No such file or directory
however if I just do it directly through the cmd line it does create the file.. so the cronjobs are just messed up with 1and1, and they won't help you at all.
u40796793:~ > date +%H%M > $HOME/test.txt
u40796793:~ > cat test.txt
0633
u40796793:~ >
Posted by: Jon | February 29, 2008 at 03:39 AM
Thanks Man!!!
I have been looking all over for how the hell to stop editing on 1and1. As another reader pointed out, 1and1's FAQ leaves you hanging.
Thanks Again,
Dan
Posted by: Dan Kelly | August 06, 2007 at 11:25 AM
actually ESC then SHIFT + Z + Z just exists WITHOUT saving for me.
i had to type ESC then :wq!
Posted by: wabisabi | July 18, 2007 at 08:46 AM
thanks man! the damn 1and1 FAQ on cron doesn't tell you how to exit the editor. Oh, OF COURSE SHIFT+Z+Z! Unix is such a kluge
Posted by: wabisabi | July 18, 2007 at 07:28 AM
I am currently trying to set up a cron job on my webserver at 1and1 Hosting as well. I had a bit of problems finding the directory that my files were in, so I am hoping that I will get it right one of these times.
I will let you all know if I get something to work.
Posted by: Adam | June 26, 2007 at 06:37 PM
This is a fantastic posting! Helps alot, and when I reproduce your example with copy files etc it works great. What I am trying to do is run a php script on my server that sends emails based on table entries. I know that the script works, because it does what I want it to when I load the page in the browser. But it doesn't do anything when I try to use it with a cron job?@!? Any ideas?
Posted by: Gui | May 18, 2007 at 12:02 PM
Alex,
Thanks for this info on crobjobs on 1&1 - I was looking for info as well and was having a hard time finding it. Certainly no thanks to 1&1 help!
I have a question for you - I am trying to set up a cronjob for a program called ProfitSponder, and I'm having a hard time doing it. Would you be willing to look at it? I would certainly be willing to pay you for your time. I just can't figure out how to make it work.
Posted by: Ryan | April 01, 2007 at 05:16 PM