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.