Monday, March 5, 2012

Linux Cron Jobs

  I have always envied Iron man for Jarvis and all other cool toys that he has. I used to envy all those people who had some or the other type of automation in their life.
  Even I wanted some automation through my computer. First I had thought of making a twitter bot that would tweet the news of the day from Wikipedia. I had also a php script to scrap the necessary data . But, then I got bored of the idea and hence dropped it in middle (nothing new for me).
  I come to my room everyday at 12:20 from my college. So, I thought, how about getting my media player ready by the time I reach my room? (Cool Idea huh!)
  I already knew that I had to simply create a cron-job for this. The question was "how?". Well, what is Google for? A simple Google search told me that I just had to enter the command
     crontab -e
  It will ask you for your favorite editor. Select the one of your choice. It opens a user cron file. Now, for people who don't know what cron file is, well in simple words, you enter the time and command and then your computer runs that command on that time. The format of a cron file is:


   m h dom mon dow command
   m-minute
   h-hour
  dom-date of month
  mon-month
  dow- date of week
  command - command to be run

  you can enter * at any field (except command ofcourse!) to run that command every minute,hour etc.
  you can also input range in the form [1-5]
  also, you can give an interval like */2 i.e occur every 2 minutes etc.
  Enter the proper value in each field and enter the correct command. And then save the file. Thats it! You have now a command that will run automatically!
  One thing though, the output of the command will not be shown on the screen but will be mailed to you.
   Also, an important thing: If you are starting any GUI application you need to give command in this format:
   env DISPLAY=:0 command
   e.g env DISPLAY=:0 firefox

   Now, who is going to keep his/her morning alarm using a cron-job? :)


No comments:

Post a Comment