Search This Blog

Wednesday, July 1, 2015

Understand at, atq, atrm, batch Commands using 9 Examples

You can execute batch jobs in UNIX / Linux using any one of the three commands — at, batch or cron.
In this article, let us review how to schedule a job, view a job, and delete a job using at command.

You can schedule an at job in two different ways:
  • Schedule the job to be executed at a specific time. For example, July 3rd, 10AM
  • Schedule the job to be executed in relative time from now. For example, 5 hours from now.

1. Schedule an at job using specific date and time

Syntax:
$ at time date
For example, to schedule a job at 11 am on May 20, use the following at command.
$ at 11 am may 20

2. Schedule an at job using relative time

You can schedule a job to be executed using relative time from now.
Syntax:
$ at now + COUNT UNIT
For example, following job will be execute 1 minute from now.
$ at now + 1 min
The above example will read the commands from stdin, and it will execute the job after a minute. When you give something wrong in time format, you will get the error ‘Garbled time‘.
You can schedule a background job for 1 hour from now, (or) 1 day from now using the following at command:
$ at now + 1 hour

$ at now + 1 day
Similar to at command, you can also use crontab to execute jobs at a scheduled time. Refer to our earlier 15 cron command examples article.

3. View all the scheduled at jobs using atq

You can use atq command (or at -l), to display all the at command jobs that are scheduled or currently running.
The following atq command will list all the pending at jobs. The first number shown is the Job number, followed by the time in which the process is to be executed, and the user name.
$ atq
4 2010-04-20 11:00 a sathiya

4. Remove/Delete a scheduled at job using atrm

You can use atrm command (or at -d), to delete a particular job. For example, to delete the job number 4, use the following atrm command.
$ atrm 4

5. Execute a job only when system load average is < 1.5 using batch command

You can schedule a job using batch command, which will prompt for command input, which will be executed when the system load average is less than 1.5.
$ batch
At the successful completion of input, you will get job number. For listing and removing batch jobs you can use the at commands explained above.

6. Schedule at jobs from file using -f option

First create a text file that contains all the commands, or shell-scripts that you would like to be executed in the background using at command.
$ cat myjobs.txt
/home/sathiya/calculate-space.sh
/path/to/a/shell-script
/path/to/any/command/or/script
Using the -f option, you can make the at command to get the input from the file instead of stdin.
Following at command will execute all the jobs from the myjobs.txt 1 hour from now.
$ at -f myjobs.txt now + 1 hour

7. Allowing and Denying certain users from using at jobs

System administrator can control who can schedule an at job and who cannot using at.allow and at.deny files.
First, system checks for at.allow file. If at.allow exists, only the usernames specified in the at.allow file are allowed to use at command.
Next, (if at.allow doesn’t exist), system checks for at.deny file. If at.deny exist, the usernames specified in the at.deny file are not allowed to use the at command.
By default, most systems uses at.deny file to stop certain users from using the at command, such as www-data, guest, backup, man user.

8. Execute at command like nohup

Similar to the nohup command we discussed earlier, you can execute a command (or shell script) on the remote server using the at command and logout from the server.
$ at -f myjob now + 1 min

$ exit
Note: myjob will still be running even after you exit out of the server.

9. Additional at command time formats

You can use any one of the following at command date time formats:
$ at 10 am tomorrow

$ at 11:00 next month

$ at 22:00 today

$ at now + 1 week

$ at noon

Tuesday, June 30, 2015

Some interesting Java Facts

Creation of an Object:

Following steps are completed while creating a new object of any class :-
1. Bind Constructor parameters.
2. If explicit this(),call recursively and skip to step 5.
3. Call recursively the implicit and explicit super(...),except for their Object class
because it has no parent class.
4. Execute the explicit instance variable initializers. // it refers to initialisation of instance variables at the time of their declaration.
5.  Execute the body of the current constructor.

Exceptions

1.finally clause is always executed regardless of the fact that exception is caused or not.
2.only way that finally will not be executed is virtual machine shutdown or execution of "System.exit(0)" method.
3.If return statement is embedded inside the try block then finally clause executes before return.
4. Exception must be thrown in same line where its object is created because it contains line no. information.

Accessing Members of "super base" Class

Suppose class B extends class A and class C further extends class B.
Now Suppose we want to access the members of class A directly through C class.
This can be either accomplished by using super twice once in C and once in B.
Or other way is to use super in Class C and if members with same name are not
present in B then this super will directly refer to member in super base class A.
example of a such a program.

class A
{
 static
 {
  System.out.println("static initializer of A");
 }
 {
  System.out.println("initializer of A");
 }
 A()
 {
  System.out.println("Constructor class A");
 }
 A(int a)
 {
  System.out.println("Constructor class A parameter");
 }
 void hello()
 {
  System.out.println("Hello in c");
 }
 void quality()
 {
  System.out.println("Quality Method Class A");
 }
}
class B extends A
{
 static
 {
  System.out.println("static initializer of B");
 }
 {
  System.out.println("initializer of B");
 }
 B()
 {
  System.out.println("Constructor class B");
 }
 B(int b)
 {
  System.out.println("Constructor class B parameter");
 }
 void show()
 {
  System.out.println("I m in class B");
 }
}
class C extends B
{
 static
 {
  System.out.println("static initializer of C");
 }
 {
  System.out.println("initializer of C");
 }
 C()
 {
  System.out.println("Constructor class C");
 }
 C(int c)
 {
  //super.show();
  System.out.println("Constructor class C parameter");
 }
 void show()
 {
  System.out.println("I m in class C");
 }
 void quality()
 {
  super.quality();
  System.out.println("Quality Method Class C");
 }
}
public class inh
{
 public static void main(String ...args)
 {
  A a=new C(3);
  a.quality();
 }
}

Polymorphic Arguments in Polymorphism

Suppose a method in a class uses reference of some base class as its argument to perform certain calculations. Now it can also intake the object of child class as its argument because child is also a type of base class object. Bu important point is that this method can only access the members of parent class and not child specific features.

Referring members of a base class in Polymorphism

If we create reference of base class then we can access child class methods which are overridden but child class specific methods are not accessible as accomplished by following program:
class base
{
 base()
 {
  System.out.println("base class");
 }
 void show()
 {
  System.out.println("Show method base class");
 }
}
class child extends base
{
 child()
 {
  System.out.println("child class");
 }
 void show()
 {
  System.out.println("Show method child class");
 }
 void specific()
 {
  System.out.println("Specific Method of Child class not present in base");
 }
 public static void main(String args[])
 {
  base b=new child();
  b.specific();//illegal will not compile
  b.show();//child class method executes
 }
}

My Profile

My photo
can be reached at 09916017317