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.
Oh my!!! You are just too true to be good. How do you manage to write and research on such wonderful things? You have inspired me to work harder now. I shall try as much as possible to enjoy life to the fullest and be satiated with the wonderful things that are around me, which I have been unaware of until now.
Posted by: Buy Generic Viagra | December 27, 2010 at 12:46 AM
A look at things from other people's ideas, the activity of mind to know other people who never have to worry for their future.
Posted by: coach outlet | December 19, 2010 at 10:41 PM
I have of late took up my own blog so its good to pickup points from what you have going here. Many Thanks.
Posted by: NFL Jersey | November 30, 2010 at 06:13 PM
so with this little modification I can left my Linux with this amazing application, fantastic I await for something like this for a long time, my Linux version has become bored, I need something to work.
Posted by: Kamagra Gel | November 12, 2010 at 12:09 PM
fantastic, only a question, this process can apply in Windows or the code change? I ask this because I want to learn to modified bith OS, this because I want to be like a freelancer in computer world, lear a little bit of everything.
Posted by: Generic Viagra | November 12, 2010 at 09:08 AM
It's so lucky for me to find your blog! So shocking and great!
Posted by: 搜索引擎营销 | November 02, 2010 at 02:09 AM
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: air max | September 14, 2010 at 02:11 AM
Special http://www.pdfspirit.com/conceitos-de- ebooks mega search system can help you find your favorite books in electronic edition in no time.
Posted by: Alena | September 02, 2010 at 07:38 AM
I agree with you. Here we know that our life have to have new thought, new type. New details can provide new really feel and new expertise to us. Just like your blog, it take me to some new state. It is so wonderful that I can not leave.
Posted by: Ugg Classic Mini | August 15, 2010 at 05:23 PM
I agree with you. Here we realize that our existence would need new strategy, new style. New factors can bring new really feel and new practical experience to us. Just like your weblog, it take me to some new state. It really is so fantastic that I can not leave.
Posted by: Ugg Classic Mini | August 13, 2010 at 07:54 PM