what is trap command and how to use it ?

I am bit interested with this command and would like to share it with u people…

If you want to perform any of the actions on receiving some signal in shell you can do that by “trap” command.

For ex, I would like to create a file when it receives TERM signal.

I can do this by below way

[root@dhcp208-213 ~]# ls |grep hi =======> There is no file exist in name “hi”

[root@dhcp208-213 ~]# trap “touch hi” TERM ===> I am making a TRAP for the “SIGTERM”

[root@dhcp208-213 ~]# trap ====> This command will show you the TRAP confuigured
trap — ‘touch hi’ SIGTERM
[root@dhcp208-213 ~]# echo $$
[root@dhcp208-213 ~]# kill -TERM 32190
[root@dhcp208-213 ~]# ls |grep hi ====> Now we have “hi” file existing in cwd..
[root@dhcp208-213 ~]#

….. hmmmm.. I will explain more..:)

Leave a Reply

Your email address will not be published. Required fields are marked *