Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Wednesday, November 28, 2012

Setting crontab with Environment variables

Though I can boast of working on Ubuntu for past 1 year, but I am not quite handy in Linux/Unix. Very small tasks create different problems to me. Yesterday I was assigned to configure a crontab expression and test one application. But I was not able to define this crontab expression due to my negligence about CRONTAB_ENV.
I think lots of people, like me, face this issue while running crontab. So let's understand crontab environment. Let’s understand the intricacies of crontab.

Crontab
Crontab is a shell script to enable scheduling. We use crontab to configure the execution of any application periodically without any human intervention.
Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab.

Different options for Crontab
crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

Understanding and Setting crontab environment
Blank lines and leading spaces and tabs are ignored. Lines whose first non-space character is a pound-sign (#) are comments, and are ignored. Note that comments are not allowed on the same line as cron commands, since they will be taken to be part of the command. Similarly, comments are not allowed on the same line as environment variable settings.

An active line in a crontab will be either an environment setting or a cron command. An environment setting is of the form,
                 name = value
where the spaces around the equal-sign (=) are optional, and any subsequent non-leading spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing blanks. Several environment variables are set up automatically by the cron daemon.

SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab’s owner. HOME and SHELL may be overridden by settings in the crontab; LOGNAME may not. In addition to LOGNAME, HOME, and SHELL, cron will look at MAILTO if it has any reason to send mail as a result of running commands in "this" crontab. If MAILTO is defined (and non-empty), mail is sent to the user so named. If MAILTO is defined but empty (MAILTO=""), no mail will be                sent. Otherwise mail is sent to the owner of the crontab.

The MLS_LEVEL environment variable provides support for multiple per- job SELinux security contexts in the same crontab.

Few environment variable can be defined in the cronfile. By default cron jobs do not consider USER defined variables, but you can explicitly tell it to use user defined variables.

0 5 * * * . $HOME/.profile; /path/to/command/to/run

Cron expression and fields


Field          Allowed Values
-----          --------------
Minute         0-59
Hour           0-23
Day of Month   1-31
Month          1-12, jan, feb, mar, apr, may, jun, jul, aug, sep, oct, 
               nov, dec
Day of Week    0-7, sun, mon, tue, wed, thu, fri, sat (0 and 7 are "sun")

So expression mentioned above says run the /path/to/command/to/run script at 5AM everyday.

Cron Security
Though each user has it's cron file, but few cron expression are accessible to different users. cron.deny and cron.allow files contain the user allowed to run an expression.

I will rest my case of cron expression now. I will update this if I get some more information about cron.

Saturday, February 5, 2011

Resizing/Increasing Root space at Ubuntu

I have been using Ubuntu for past couple of months and my experience has been great. Till now I have not done much of the experiment with it but I have encountered few problems and resolved few as well.

As I am a Novice in Linux I understand the severity of the problem faced by us. They might seem simple to people who are already in business for years but for people like us those issues are pain in A**.

Today I am gonna discuss one problem which is faced by people who start Linux as experiment and later start loving it.

Problem
My current laptop was equipped with Windows Vista. I wanted to experiment with some flavor of Linux but I was not sure of removing Windows, so windows survived primarily as a backup plan. At the time of installation my HDD dint have much space, But as I was experimenting so I did not think of it as an Issue. I installed Ubuntu on my system and as per the availability of the resources It assigned 6.1 GB of disk space to it. With the time my data increased in Home directory and suddenly I found myself without even I MB space to do my day to day activities. Horrible, is not it? But I solved issue by removing few files here and there and got it working for next couple more days. What next? Hmmm, Now I have to solve this problem.

So problem is to either add size in my Ubuntu partition or reinstall it again with little extra space?

Solution
Oops, second option is scary. I asked one kid(I am still a kid :) :) ) and he said, Uncle gone mad, resize it :). So did I. First of all I removed some movies from my collection.

As I was still having windows, so I created raw disk using windows.
- RAW Disk is something which is not even recognized to windows.

Create EXT4 partition using Unix GUI utility.
Now resizing Ubuntu there were two option. Increase my bootable partition or assign new partition to one folder. Increasing bootable partition need live CD and I wanted some fast recovery. So I used another option.

Steps to be followed:

1. Create a EXT4 partition and LABEL it, e.g. SAMPLE.

2. Go to Root using terminal.
cd /

3. Create a New Directory, e.g home_new
mkdir home_new

4. Modify fstab( /etc/fstab) such that new partition will point to /home_new. Add following entry.
sudo vi /etc/fstab
Pass the root password and add the entry in the file.
LABEL=SAMPLE  /home_new ext4 defaults,nodev,nosuid,relatime 0 2
If you want to read more about fstab entry, then click here .

5. Move whole data from /home to /home_new. It is better to cross verify if the data has been moved or not. If the data is copied to /home_new and still exist in /home then remove this data.
sudo mv /home/* /home_new



5. After coping and removing data exist in /home, Modify fstab( /etc/fstab) such that new partition will point to /home. Modify previous entry to look like this
sudo vi /etc/fstab
Pass the root password and add the entry in the file.
LABEL=SAMPLE  /home ext4 defaults,nodev,nosuid,relatime 0 2



Now you can restart your computer. Now your home is pointing to a different disk and you have more space in your system.

In between I have asked to recheck if data is removed or not. I have specified that because if you are not deleting the data in old home directory then after making all changes this data will not free required space and you will not be able to delete it later. As I did not delete the data, so currently I am not able to use 1.5GB of my hard disk.

I have to figure out a way to retrieve or delete this data and currently it is an open problem for me. Hope this article will help you to add size to your ubuntu version without making much efforts.