Tuesday, January 10, 2012

Cron Jobs and common issues with cron jobs


Cron jobs are commands that are run at a specified interval and hence they are difficult to troubleshoot. Cron searches its spool area (/var/spool/cron/crontabs) for crontab files (which are named after accounts in /etc/passwd); crontabs found are loaded into memory. Note that crontabs in this directory should not be accessed directly - the crontab command should be used to access and update them. The cron daemon runs every minute and checks for stored crontabs and commands to see if there are any that need to be run in that minute. If it finds one it is run. 


Common issues: 


1. If you are running a script via cron job which uses relative paths then your script won't be able to process those relative paths. You need to use complete path's. For e.g., if your script is trying to open a file abc.txt you should modify your script so that the file path is complete like: /home/myuser/abc.txt otherwise your script will not be able to find this file. 


2. Strict Permissions - Make sure all scripts/files/folders are given execute permissions. Use command chmod +x <filename> to give write permissions. 


3. To troubleshoot cron job you can modify your crontab file to include MAILTO=email@email.com with your email address. When cron job is executed the result/errors etc are sent to this email id and hence they are useful in troubleshooting. 


4. When cron job is run from the users crontab it is executed as that user. It does not however source any files in the users home directory like their .cshrc or .bashrc or any other file. If you need cron to source (read) any file that your script will need you should do it from the script cron is calling. Setting paths, sourcing files, setting environment variables, etc. need to be done from your script itself. 


5. If the users account has a crontab but no useable shell in /etc/passwd then the cronjob will not run. You will have to give the account a shell for the crontab to run.


6. If your cronjobs are not running check if the cron deamon is running. Then remember to check /etc/cron.allow and /etc/cron.deny files. If they exist then the user you want to be able to run jobs must be in /etc/cron.allow. You also might want to check if the /etc/security/access.conf file exists. You might need to add your user in there.


7. Cron does not deal with seconds so you can't have cronjob's going off in any time period dealing with seconds. Like a cronjob going off every 30 seconds.

Saturday, January 7, 2012

Calculate difference between two dates in bash?


The easiest way is to convert both the dates to unix timestamp (which is the number of seconds from Jan 1, 1970). Once done simply substract them and divide by 86400. 


For e.g., date --date "2012-01-06" +%s Converts the date to number of seconds..



d1=`date --date "2012-01-06" +%s`
d2=`date +%Y-%m-%d`
d2=`date --date "$d2" +%s`


diff=$((d2-d1))
days=`echo $((diff/86400))`

The variables days will contain the difference in days between d1 and d2. Where, d1 is 2012-01-06 (yyyy-mm-dd format) and d2 is current date. 

Enjoy.

Converting date time String to only date format in Bash


Suppose you have a string like 2012-01-06 14:38:36 and you want to get only the date part of it... then try the following simply date command


VAR="2012-01-06 14:38:36" 
VAR=`date +%Y-%m-%d -d "$VAR"`
echo $VAR


This will convert the string to only date and output: 


2012-01-06

Tuesday, January 3, 2012

Copying or Transferring Windows AMI across Amazons EC2 regions


The process is simple but long & tiring… Here is a brief note on how to do it:

SR (Source Region) – Where you already have an AMI that you wish to transfer to other region.
DR (Destination Region) – Where AMI need to be transferred to.

SR:
1.       Create an Instance from the AMI.
2.       Note down the volume used by this AMI.
3.       Shut down and Detach the volume.
4.       Now, create one ubuntu instance and attach the volume from step 3 to this instance.

DR:
1.       Create a blank volume of same size as that of SR point 2.
2.       Create a ubuntu instance and attach the volume from point 1 above to this instance.

Now, all we need to do is use the ‘dd’ command to binary transfer the volume from SR to the blank volume in DR. So here is the command that you will run on SR:  Make sure you have given full permissions to /dev/sdh1 or /dev/xvdf for newer versions.

sudo dd if=/dev/sda1 | ssh -i mykey.pem instancepublicdns.compute.amazonaws.com dd of=/dev/sdh1

Here:
             /dev/sda1 may need to be /dev/xvdf for newer versions
            Mykey.pem is the key that was used to create DR ubuntu instance.
            Instancepublicdns.compute.amazon.com is the public dns of DR’s ubuntu instance.

The dd command does a binary transfer and takes hell lot of time depending on the size of volume and network speed. Worst to that is that it does not display any progress indicator etc.. It only shows the results when it is done.

Once the dd command is over follow below steps:
1         .    Detach the blank volume that was created in DR Point 1.
2         .    Create a Windows Instance and shut it down.
3         .    Detach the default volume of Windows instance created above.
4         .    Attach the blank volume we created in DR Point 1.
5         .    Reboot Windows instance and you are good to go.

Now, you can create an AMI from this Windows Running instance which will be an exact copy of what you had in your source region. 

Monday, January 2, 2012

Unix File permissions

File permissions of any file/directory are controlled using chmod command. The chmod command uses a three digit code as an argument. These three digits set the permissions. Any file/directory in Unix has the following groups/users:

1. Owner
2. Group (Group of users that has been set up)
3. World (anyone else browsing on the file system)


Each digit of this code sets permissions for one of these groups as follows. Read is 4. Write is 2. Execute is 1



The sums of these numbers give combinations of these permissions:
·       0 = no permissions whatsoever; this person cannot read, write, or execute the file
·       1 = execute only
·       2 = write only
·       3 = write and execute (1+2)
·       4 = read only
·       5 = read and execute (4+1)
·       6 = read and write (4+2)
·       7 = read and write and execute (4+2+1)

E.g., 
ubuntu@ip-10-134-173-143:~$ ls -al
-rw-r--r-- 1 ubuntu ubuntu  220 2011-05-18 09:54 .bash_logout
ubuntu@ip-10-134-173-143:~$



The first "-" above tells that bash_logout is a file. The first three letters "rw-" tells that the file has read & write permission for its owner. Then the next three symbols, r--, show that the group permissions are read only. The final three symbols, r--, show that the world permissions are read only.