You can terminate a process using the KILL command. To KILL a process you need to have the process ID passed to the KILL command.
There are different types of signals that you pass to a process.
1 SIGHUP - Hang Up
2 SIGINT - Interrrupt
9 SIGKILL - Kill Immediate
15 SIGTERM - Terminate
SIGHUP signal is sent to every process you are running just before you log out of the system.
SIGINT signal is sent when you press ctrl+c
SIGKILL - Kills a process immediately. Temporary files are not deleted here.
SIGTERM - Immediate termination of progamme but allows cleaning up the temporary files.
The default signal with KILL command is SIGTERM.
You can specify the signals with KILL command either by their number or by their name without the SIG
eg:-
kill -9 process.sh
or
kill KILL process.sh
Basically there are 30 different signals in UNIX. The above explained are the most important ones.
Example:
Finding the jobs
bash-3.00$ jobs
[2]- Stopped vi
[3]+ Stopped vi
Killing the job with % (here we terminates the job)
bash-3.00$ kill %2
bash-3.00$ jobs
[2]- Done vi
[3]+ Stopped vi
Finding Jobs:
bash-3.00$ jobs
[3]+ Stopped vi
Killing jobs with KILL signal (-KILL is missing)
bash-3.00$ kill KILL %3
bash: kill: KILL: arguments must be process or job IDs
starting a process in the background
bash-3.00$ vi &
[1] 28236
bash-3.00$ jobs
[1]+ Stopped vi
Kill process with signal number (kill immediate)
bash-3.00$ kill -9 28236
bash-3.00$ jobs
[1]+ Killed vi
Checking for jobs
bash-3.00$ jobs
No jobs left
create new job in bg
bash-3.00$ vi &
[1] 28321
bash-3.00$ jobs
[1]+ Stopped vi
Kill with signal number option for termination
bash-3.00$ kill -15 28321
bash-3.00$ jobs
[1]+ Stopped vi
bash-3.00$ kill 28321
bash-3.00$ jobs
[1]+ Stopped vi
bash-3.00$ kill -KILL 28321
bash-3.00$ jobs
[1]+ Killed vi
Note: Inorder to run the kill command for a second time to make sure that the kill works, enter "!!"
Tuesday, November 22, 2011
UNIX: Find the running processes
Finding out Jobs that runs in your UNIX system is a simple task. The command, jobs, helps you to list the jobs which are either stopped or moved to background for process.
Below code automatically moves the vi process to the background
bash-3.00$ vi &
[1] 27467
On entering hte "jobs" command it listed the jobs that are stopped in the background.
bash-3.00$ jobs
[1]+ Stopped vi
bash-3.00$
Alternative to find the processes running in your UNIX system:
The command ps is another one alternative to find the jobs running in your UNIX system. This is a little complex. The command ps stands for processor status and it lists out the processor status of the entire computer.
bash-3.00$ ps
PID TTY TIME CMD
27467 pts/15 0:00 vi
27469 pts/15 0:00 ps
3784 pts/15 0:01 bash
3780 pts/15 0:00 sh
Below code automatically moves the vi process to the background
bash-3.00$ vi &
[1] 27467
On entering hte "jobs" command it listed the jobs that are stopped in the background.
bash-3.00$ jobs
[1]+ Stopped vi
bash-3.00$
Alternative to find the processes running in your UNIX system:
The command ps is another one alternative to find the jobs running in your UNIX system. This is a little complex. The command ps stands for processor status and it lists out the processor status of the entire computer.
bash-3.00$ ps
PID TTY TIME CMD
27467 pts/15 0:00 vi
27469 pts/15 0:00 ps
3784 pts/15 0:01 bash
3780 pts/15 0:00 sh
Monday, November 21, 2011
UNIX Process or Jobs: Suspend and Resume
In unix whatever command you execute from the command line initiate a process. Say for example you tried ls tol list files invokes the process ls. In UNIX any program that's running is a process. You can have multiple process running at once.
Any program that you are running is known as current job.
States of Jobs/Process:
There are different states for the Jobs/Process in UNIX. The most important one is "running".
Stopping and restarting a Job:
A job can be stopped by pressing ctrl+z and to restart it enter fg.
bash-3.00$ ./logging.sh
Stopping the server please wait .........\n
Tue Nov 22 12:08:52 IST 2011
Invoking the PL/SQL Procedures and Bulk add command \n
Bulk Add Start Time:
Tue Nov 22 12:08:52 IST 2011
^Z
[1]+ Stopped ./logging.sh
bash-3.00$ fg
./logging.sh
Bulk Add End Time:
Tue Nov 22 12:09:09 IST 2011
Done with PL/SQL Procedures and Bulk add. Starting the server. Please wait....\n
Tue Nov 22 12:09:09 IST 2011
1
bash-3.00$
Why we use ctrl+z to stop and fg to restart it later?
These commands helps us to stop a a job or process (ctrl+z) that we are doing currently and perform some other job and then resume (fg) our stopped job later.
For example if we are working on a vi editor editing our file and in between you want to execute some other programme press ctrl+z and once after completing your second job you can resume to editing your file by entering fg.
bash-3.00$ vi
I am testing stopping job and resuming it later
let me stop the job now !!
i am pressing ctrl+z!! ^Z^Z^Z^Z^Z^Z
ooops!! Before pressing ctrl+z we should be in command mode!!
Moving to command mode and then pressing ctrl+z
~
~
~
Incomplete shell escape command - use 'shell' to get a shell
[1]+ Stopped vi
bash-3.00$
bash-3.00$ ls -l | tail -5
-rw-r--r-- 1 fwire fwire 1331 Apr 20 2011 publishpreview.sh
-rw-r--r-- 1 fwire fwire 11 Feb 25 2011 publishpreview_dummy.sh
-rw-r--r-- 1 fwire fwire 275 Oct 15 17:28 shiyas_new.txt
-rw-r--r-- 1 fwire fwire 1622 Oct 15 16:35 test.sh
-rw-r--r-- 1 fwire fwire 313 Oct 15 17:15 uniqtest.txt
bash-3.00$fg
I am testing stopping job and resuming it later
let me stop the job now !!
i am pressing ctrl+z!! ^Z^Z^Z^Z^Z^Z
ooops!! Before pressing ctrl+z we should be in command mode!!
Moving to command mode and then pressing ctrl+z
I am back !!
~
Any program that you are running is known as current job.
States of Jobs/Process:
There are different states for the Jobs/Process in UNIX. The most important one is "running".
Stopping and restarting a Job:
A job can be stopped by pressing ctrl+z and to restart it enter fg.
bash-3.00$ ./logging.sh
Stopping the server please wait .........\n
Tue Nov 22 12:08:52 IST 2011
Invoking the PL/SQL Procedures and Bulk add command \n
Bulk Add Start Time:
Tue Nov 22 12:08:52 IST 2011
^Z
[1]+ Stopped ./logging.sh
bash-3.00$ fg
./logging.sh
Bulk Add End Time:
Tue Nov 22 12:09:09 IST 2011
Done with PL/SQL Procedures and Bulk add. Starting the server. Please wait....\n
Tue Nov 22 12:09:09 IST 2011
1
bash-3.00$
Why we use ctrl+z to stop and fg to restart it later?
These commands helps us to stop a a job or process (ctrl+z) that we are doing currently and perform some other job and then resume (fg) our stopped job later.
For example if we are working on a vi editor editing our file and in between you want to execute some other programme press ctrl+z and once after completing your second job you can resume to editing your file by entering fg.
bash-3.00$ vi
I am testing stopping job and resuming it later
let me stop the job now !!
i am pressing ctrl+z!! ^Z^Z^Z^Z^Z^Z
ooops!! Before pressing ctrl+z we should be in command mode!!
Moving to command mode and then pressing ctrl+z
~
~
~
Incomplete shell escape command - use 'shell' to get a shell
[1]+ Stopped vi
bash-3.00$
bash-3.00$ ls -l | tail -5
-rw-r--r-- 1 fwire fwire 1331 Apr 20 2011 publishpreview.sh
-rw-r--r-- 1 fwire fwire 11 Feb 25 2011 publishpreview_dummy.sh
-rw-r--r-- 1 fwire fwire 275 Oct 15 17:28 shiyas_new.txt
-rw-r--r-- 1 fwire fwire 1622 Oct 15 16:35 test.sh
-rw-r--r-- 1 fwire fwire 313 Oct 15 17:15 uniqtest.txt
bash-3.00$fg
I am testing stopping job and resuming it later
let me stop the job now !!
i am pressing ctrl+z!! ^Z^Z^Z^Z^Z^Z
ooops!! Before pressing ctrl+z we should be in command mode!!
Moving to command mode and then pressing ctrl+z
I am back !!
~
Wednesday, October 26, 2011
Shell Variables
Working with Shell variables.
As like any other programming languages shell also has variable (most people dont know we can do programming with shell commands.). The shell variables takes single values as strings, even the numeric values is also considered as string.
Declaring and assigning shell variable:
You can declare and assign shell variable as simple as below.
bash-3.00$ color=yellow
You need to be careful while doing this. There should not be any space before and after equal to sign '=', if exists throws error.
Referencing a shell variable:
Below command will display the value stored in the variable color
bash-3.00$ echo $color
yellow
what is the output?
bash-3.00$ echo $coloryish
As no variable is declared as coloryish the output will be null. If you are actually interested to display "yellowyish", place the variable in curly braces as below.
bash-3.00$ echo ${color}yish
yellowyish
What if instead of variable color we supplied colour, which is not defined?
bash-3.00$ echo The dress is ${colour}ish
The dress is ish
How to handle this? Fortunately shell helps us to have default values to be set in case the variable is not defined as below
bash-3.00$ echo The dress is ${colour:-green}ish
The dress is greenish
The default value will come into effect only if the variable referenced is not declared. The syntax :- tells the shell to print the following character
Assigning Variables with read command:
We can assign values to multiple variables at a time with read commnad.
bash-3.00$ read city state message
Thazhekode Kerala Hi Mom!
The above command declares three variables city, state and message and assign values respectively.
city=Thazhekode
state=Kerala
message=Hi Mom!
bash-3.00$ read city state message
Thazhekode New Jersey Hi Mom!
city=Thazhekode
state=New
message=Jersy Hi Mom!
Read command assigns an individual word to a specified variable, and the remaining words are assigned to the last variable. To handle this situation you can use the escape character
bash-3.00$ read city state message
Thazhekode New\ Jersey Hi Mom!
As like any other programming languages shell also has variable (most people dont know we can do programming with shell commands.). The shell variables takes single values as strings, even the numeric values is also considered as string.
Declaring and assigning shell variable:
You can declare and assign shell variable as simple as below.
bash-3.00$ color=yellow
You need to be careful while doing this. There should not be any space before and after equal to sign '=', if exists throws error.
Referencing a shell variable:
Below command will display the value stored in the variable color
bash-3.00$ echo $color
yellow
what is the output?
bash-3.00$ echo $coloryish
As no variable is declared as coloryish the output will be null. If you are actually interested to display "yellowyish", place the variable in curly braces as below.
bash-3.00$ echo ${color}yish
yellowyish
What if instead of variable color we supplied colour, which is not defined?
bash-3.00$ echo The dress is ${colour}ish
The dress is ish
How to handle this? Fortunately shell helps us to have default values to be set in case the variable is not defined as below
bash-3.00$ echo The dress is ${colour:-green}ish
The dress is greenish
The default value will come into effect only if the variable referenced is not declared. The syntax :- tells the shell to print the following character
Assigning Variables with read command:
We can assign values to multiple variables at a time with read commnad.
bash-3.00$ read city state message
Thazhekode Kerala Hi Mom!
The above command declares three variables city, state and message and assign values respectively.
city=Thazhekode
state=Kerala
message=Hi Mom!
bash-3.00$ read city state message
Thazhekode New Jersey Hi Mom!
city=Thazhekode
state=New
message=Jersy Hi Mom!
Read command assigns an individual word to a specified variable, and the remaining words are assigned to the last variable. To handle this situation you can use the escape character
bash-3.00$ read city state message
Thazhekode New\ Jersey Hi Mom!
Tuesday, October 25, 2011
UNIX: Shell scripts
Definition: Shell scripts are a series of shell commands added in a file in exactly the order in which they’ll be
executed. Upon giving execute permission for this file, you can execute this entire set of commands by just entering the file name as like a command.
Example:
This example script will search for a specified file in all dirctories.
UNIX: Setting custom prompts
The below command is based on ksh
$ bash
bash-3.00$ PS1='Order me$'
Order me$
$ bash
bash-3.00$ PS1='Order me$'
Order me$
UNIX Command Aliases
With alias, you can define new commands that does whatever you like to do with unix in a customized way.
Format for using aliases in
csh:- alias word command-sequence
ksh:- alias word='command'
Example:
bash-3.00$ alias shiyas='ls -CF'
If you want to see the list of aliases created, you only need to enter the word alias
Example:
bash-3.00$ alias
alias ls='ls -CF'
alias shiyas='ls -CF'
Note:
There should not be any space befor or after '=' sign.
If you want to make availability of an alias permanent add it into .chrsc if you are using csh or to .profile if you are using korn shell
Format for using aliases in
csh:- alias word command-sequence
ksh:- alias word='command'
Example:
bash-3.00$ alias shiyas='ls -CF'
If you want to see the list of aliases created, you only need to enter the word alias
Example:
bash-3.00$ alias
alias ls='ls -CF'
alias shiyas='ls -CF'
Note:
There should not be any space befor or after '=' sign.
If you want to make availability of an alias permanent add it into .chrsc if you are using csh or to .profile if you are using korn shell
Subscribe to:
Posts (Atom)