Thursday, 3 April 2014

Setting up a cron job on Ubuntu (Or any Linux Distro)

Alright In this blog post. I will show you how to set up a Cron job in ubuntu. (Note the same steps can be used to setup a cron job on any other linux distro as well.)

Ok, Now time for some basics about what a Cron Job is.

    Essentially a Cron Job helps to to perform a predefined task (or a set of tasks) at time intervals set by you (The User).
     So What is the need for setting up a Cron Job you ask ???

  • Well a vague example would be to set a script that automatically backs-up you home folder say at 12:00 AM every day.
  • Another example would be to get your external IP and set it to your drop-box, so that you can access your computer or say view a hosted web site (Assuming you are broke as me and cannot afford a domain :P ;) )
  • So on and so forth.

Now that we are done with what is a Cron job and with a possible idea of when to use one lets get right into it and set-up a cron job that basically does nothing but print to a file every minute. 
(I know this is really stupid but it serves as a good example and hopefully help you build off of)

The Environment that I have is a Ubuntu 12.04 LTS, so if you have a similar set-up then just follow along and rest assured that things will work like magic. Even if you are on any other distro the steps would still be the same and wont be too hard to follow along ( I hope).

  1. STEP ONE:- CRON JOB INSTALL
  2. STEP TWO:- SETUP A CRON JOB
  • The Process that I will be talking about is setting up a cron job using a Cron file.
  • (Why this and not using corn-tab -e, as most other tutorials show,) Well that is exactly why, I wanted this to be a little different from the others and I feel this is a lot easier to understand and to expand upon as well)
  • Ok down to the real deal. Open up a Console(ctrl+alt+t for ubuntu users). 
  • First stop create a crontab file any where you please. I would do so in the root of your home directory,( Note that this cron job will be available for this user only). Like So touch crontab.txt 
  • As far as I know almost all linux distros come with the cron utility pre installed (Nothing to do yay!!)

Thursday, 22 August 2013

Bash Tricks. Ailases.


Are you using a Linux machine ? Are you lazy to type in the commands like "sudo apt-get install #package-name" to install software ?? If yes, then read on.

The basic idea is to create a small hack that you can use to get rid of the nasty "sudo apt-get install" (for Debian based operating systems). But that being said this tutorial can be used by other 'non-debian' distro's as well !!


There may  be other ways of achieving the same result but the one that I'm using here is through "bash". (You can visit the link to get a good understanding of what bash is and what it is capable of doing).
 
Why bash ?
  • A it comes pre-installed with all Linux-Unix based Operating systems.
  • It's really simple to alter the way that you use some of the standard Linux commands.(As you will see shortly).

 What tools do I need to install to follow the tutorial ?
  •  Well if you have a Linux distro that would be all that you would need.

  Ok I have a Linux distro what next ?
  • Great !!! without further ado lets get hacking :)

Steps involved:-

  1. Open your file manager. And go to your home folder.(Usually it has the same name as that of the currently logged in user).
  2. Show hidden files by pressing "ctrl+h". And look for ".bashrc" file.(If you don't have it just create one.)
  3. Open the .bashrc file in your favorite text editor.
  4. Scroll down to the section which say "aliases. (If you created the file by your self then just add the following lines).
  5. Add an alias that you want. In this case we want to make the "sudo apt-get install" shorter, hmmm lets replace all that only by "install". 
  6. Add the following. [alias install  = "sudo apt-get install"]. Note do not include the braces.
  7. Save the file and exit out of it.
  8. Next we would want to load this setting by default when ever you login or do a fresh boot.
  9. Going back to your home folder. Create a file and call it ".bash_profile" (Note do not include the inverted commas).
  10. Open the ".bash_profile" and paste in the following code. "[ -f $HOME/.bashrc ] && . $HOME/.bashrc". (Note do not include the inverted commas).
  11. Save this file and exit out.
  12. Open terminal make sure your are in the home path. And source the .bash_profile that you just created by typing the following command in the terminal.
  13. "source ~/.bash_profile". (Note do not include the inverted commas").
  14. Add viola your done. Logout and log back in and install new software by typing "install #package_name".