Wednesday, May 25, 2016

what is the use of nohup command in linux ?


Most of the time you login into remote server via ssh. If you start a shell script or command and you exit (abort remote connection), the process / command will get killed. Sometime job or command takes a long time. If you are not sure when the job will finish, then it is better to leave job running in background. But, if you log out of the system, the job will be stopped and terminated by your shell. What do you do to keep job running in the background when process gets SIGHUP?
 nohup command line-utility which allows to run command/process or shell script that can continue running in the background after you log out from a shell:

nohup command syntax:
The syntax is as follows
nohup command-name &

OR
nohup /path/to/command-name arg1 arg2 &

Where,

command-name : is name of shell script or command name. You can pass argument to command or a shell script.
& : nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an & symbol.
Use jobs -l command to list all jobs:
# jobs -l

nohup command examples

First, login to remote server using ssh command:
$ ssh user@remote.server.com

OR
$ ssh rajesh@linuxforfreshers.com

I am going to execute a shell script called ftp.sh:
# nohup ftp.sh &

Type exit or press CTRL + D exit from remote server:
# exit

In this example, I am going to find all programs and scripts with setuid bit set on, enter:
# nohup find / -xdev -type f -perm +u=s -print > out.txt &

Type exit or press CTRL + D exit from remote server.
# exit

Please note that nohup does not change the scheduling priority of COMMAND; use nice command for that purpose. The syntax is:
# nohup nice -n -5 ls / > out.txt &


As you can see nohup keep processes running after you exit from a shell. Read man page of nohup(1) and nice(1) for more information. Please note that nohup is almost available on Solaris/BSD/Linux/UNIX variants.

Tuesday, May 24, 2016

How to Get Current Time In Shell Script ?


How do I get the current server time in shell script on Linux or Unix-like operating systems? 
How do I store the current time in the shell variable and use in my scripts?

You can use the date command to display or set the current date and time. You need to use the date FORMAT syntax to controls the output of the date command. The %T format sequence interpreted by the date command to display the current time. The syntax is:


date +%FORMAT
date +"%FORMAT"
var=$(date +"%FORMAT")

Example: Show current time

Open a terminal and type the following command:
date +"%T"
Sample outputs:
13:24:21
To store time to a shell variable called now, enter:
now=$(date +"%T")
echo "Current time : $now"
Sample outputs:
Current time : 13:24:21
date +%FORMAT
date +"%FORMAT"
var=$(date +"%FORMAT")

Example: Show current time

Open a terminal and type the following command:
date +"%T"
Sample outputs:
16:33:22
To store time to a shell variable called now, enter:
now=$(date +"%T")
echo "Current time : $now"
Sample outputs:
Current time : 13:31:55

Example: 12 hour clock time

Pass the %r format to the date command:
date +”%r”
Sample outputs:
”12:25:43 PM”
To remove AM or PM from the output use, type:
date +"%I:%M:%S"
Sample outputs:
12:26:27


Tuesday, May 10, 2016

tee command examples in linux?


In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters.




Image result for tee command in unix



Example 1: Write output to stdout, and also to a file

The following command displays output only on the screen (stdout).
$ ls
The following command writes the output only to the file and not to the screen.
$ ls > file
The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file.
$ ls | tee file

Example 2: Write the output to two commands
You can also use tee command to store the output of a command to a file and redirect the same output as an input to another command.
The following command will take a backup of the crontab entries, and pass the crontab entries as an input to sed command which will do the substituion. After the substitution, it will be added as a new cron job.
$ crontab -l | tee crontab-backup.txt | sed 's/old/new/' | crontab  -e

Misc Tee Command Operations

By default tee command overwrites the file. You can instruct tee command to append to the file using the option –a as shown below.
$ ls | tee –a file
You can also write the output to multiple files as shown below.
$ ls | tee file1 file2 file3