Shell Scripting Tutorial for Beginners 27 - Signals and Traps


hey guys welcome to the next blog and shell scripting tutorial for beginners in this blog we will see how to use trap statement in shell scripting now the trap statement is relatively tough to explain so I'm going to take an example and then I will show you how we can use trap and why we should use stop okay so I have an example script here and the first line of this script is to print the PID of the script itself okay so dollar dollar symbol you can use to print the PID of the script itself right now the other code here is a while loop and basically what it's doing is it's just looping around and I have provided the 10-second delay in every loop so that you know after every 10 second it's going to loop around and print the value of count here until and unless the value is less than 10 and if the script is run successfully after for example 100 seconds we will just exit out of the script with the exit signal 0 right so let me run the script and let's see what happens so I am going to run the script and you can see the PID of this script is printed here right and then this script goes into this loop and after every 10 second is going to print the value of count which is for example one and then two and three and then four and five right now while running your script some scenarios can happen so while running your script user may press for example ctrl C keys in order to you know come out of the script so when you press control C to terminate the script is going to be terminated and our script is in the middle of doing something but we have provided this control-c signal and then the script is ended right now this control-c signal is called the interrupt signal and in signal terms it's called sig IND okay so we have sent the sig int command to the script and then the script is terminated right once again I am going to run the script and we can use some other command soon right now the different PID is provided to this script while it's running right before it was this PID and now this script is running with this PID right and we can press control read also so control Z and it's also going to stop the script and this control Z signal is called suspend signal okay and it's in signal terms it's called sig t s T P okay so you can see when I press control Z the script is stopped and now we are out of the script but this is also a interrupt signal so we have interrupted our script because our script was in the middle of doing something and we have given some signal to stop it right now now once again I'm going to run the script for example so this time the PID is this one and once again it's running and I can kill this process by a kill command also so I know the PID of this process for this script so I can just use another terminal so this is the different terminals than this right so I can use the another terminal and I can just give a kill and then minus 9 and this minus 9 is also a signal and it's called cig kill signal right so this is a cig this is a kill come on and this nine stand for six kill signal and then I can give the name of the PID which is three seven eight four in this case you can see this is the PID right now right and when I give this kill command press Enter you can see our script is killed once again it was in the middle of doing something but we have given a signal to it in this case we have given a sig kill signal and then the script is stormed right so there are some scenarios in which signal as our script is interrupted in the middle of doing something and it's not yet finished but some signal can come or some unexpected behavior can come which can interrupt the execution of our script and that's why we use trap command now trap command provides the script to capture and interrupt and then clean it up within the script okay so now when we use the trout come on our tab command in the script will be able to you know catch this kind of for example kill command or control C or control said so it's going to trap or catch this kind of signals and then we will be able to do some kind of stuff before exiting out right so that's why we use the trap command now we will take an example for the trap but let me show you something so I'm going to just open the different terminal for example and this is a big terminal and here I'm going to write man seven and then signal write signal and then press Enter and when I see the main page of the signal then I will be able to see more you know explanation about these signals right now when I scroll a little bit down I will be able to see this kind of table I was talking about right so control see signal I have given in my case right so control see signal was this one interrupt from keyboard I have given interrupt from keyboard which is control C and this is the signal for that which is sig IND and every signal has its value so SIGINT has value to right and then we have provided control Z also right which was sig T STP you can see this one was the signal right and this stands for stop tied at comma at terminal right it has the value 18 20 and 24 okay and then we have provided this fig kill command and it has the value 9 so we have used kill minus 9 so this was the value of this signal we have we could have used sig kill also and the value of this is 9 right so we have given kill 9 so it was killed okay and these are some of the signals you can use to terminate your you know process right so if you want to read more about baby signals then just do man 7 and signal okay man 7 and the key words signal I'm going to just close this terminal and let's take an example now okay so now what I want to do is I want to remove everything here and then what I need to do is I will just use a trap come on just write crap and then I will echo something so for example I can just take oh that some exit command is detected right so I can just write a great fund is detected okay and then I am expecting the signal zero from it okay so exact zero is a success signal okay so you have shown you the signals have their value and they are always greater than zero right so if the signal sent here is zero then it's a success right so now what I am going to is I will just echo for example hello world okay and I just want to exit out of the script side I can just do exit zero exits zero is a success signal right so zero is a success signal and let me clear the terminal here also clear and let me run the script once again and when I screw on the script I can see here that the script is starting so what what is the execution sequence here the script started it has seen that there is a trap for some signal and trap means whenever we receive a signal 0 then we need to execute this kind of command whatever command is here it will be executed whatever come on you right after the trap signal ok so the command is echo exit command is detected so this this is the command command echo is used and then this is printed when this signal is cached which is on exit 0 you know command right now here once again I'm going to just cut this code and I'm going to go back to our previous code so for example we had this code right and then we can write trap here so crap and then we can once again echo something so this time for example we are expecting some other signals so for example we want to detect that it's a fig in signal we can just write sig int here or we can even write its value to okay to is the value of sig in signal and this second signal is for you know delay it's for interrupt the thing is interrupting the sequence right which is control C so when I run the script once again the script with it will start with some PID and then I will just press control C and then see what happens so it's going to print this value so whenever the signal is received is going to execute whatever command we right after the trap command so I'm going to just press control C and you can see you it's printed a great signal is detected once again control C and it says exit signal is detected right and you can see right now it's not you know coming out of the terminal and giving this control C command so many times and it has completed its execution and you know every time when I press control C it's prints exit signal is detected right let me clear the terminal once again and now this time I want to catch the sig kill come on so you can either write sig kill here or either write 9 the value of sig kill okay so cigs kill come on and then I will just run the script and I know the PID of it so I can just use the skin command with -9 signal and then the PID of it which is four to nine six four two nine six right and then press ENTER and you can see this process is killed and this time we don't you know see this exit message you can see it's killed but I told you that we can trap any command any signal and we can execute any command when this signal is received and I have send the signal and nothing is printed right so there is some exceptions to the trap okay so trap cannot trap or catch sig kill command and sig stop command okay so these two you know signals it cannot catch so whenever you write sig kill or sig stop then trap is not going to catch them it's unable to catch them okay so these are the two exceptions okay so don't use a sig kill or don't catch say kill or sig stop from your trap command now let's take one more example and let's understand how it works okay so what I'm going to do is I'm going to just going to my desktop or directory and I will do LS there okay so there is only one script file there which is my X script itself right so I'm going to create one more file which is while dot txt and this file is created I'm not going to insert any data inside it I will just LS once again and now we can see that this file is there okay now I will do PWD to know in which directory we are in right and for example this time what we want to do is whenever we receive any signal we want to delete this file somehow okay so how can we do it so I'm going to declare a variable file for example and then I'm going to give some value to this file so I will just take the complete path of this directory desktop directory and the name of the file right so just copy this and paste it here okay and this time instead of echoing something I will just do RM minus F to force the deletion and then the file variable name which is file itself okay and you can after removing you can exit out of the script so you can just give the semicolon and exit command to exit out of the script right so this semicolon is used to combine two commands if you know how they works right now I want to catch some signals so for example I want to catch the sacrifice signal which is zero and then for example I want to catch the control-c signal which is two second and then for example I want to catch the signal 15 which is sig term signal which is termination signal or even you can do nine also for example for sig kill or any other signal right but nine will not work here because sig kill is an exception right so I want to just remove this file whenever I send either of these signals I can do this right so I'm going to run the script and script is running and now I will just press control C for example or let me just give this 15 which is a fig term so I know the PID so I can just give kill minus 15 and the PID which is 1 which is 5 6 3 3 right and then plus enter this signal is received in this script and then we are exited out of the script and let's do LS once again and you can see that this file is removed ok so you can do that also so if you want to remove a file or you want to do any cleanup you can do with the command and then you can just catch that signal and then do the cleanup right and you could have done something like this so if the file is removed successfully you could have used for example and here and then you could have echoed the for example while deleted right so you can just write file deleted something like this also you can do ok to know that this trap is executed ok so once again I am going to touch this file and I will do LS now once again this while a exists and once again I am going to run the script and script is running and this time the PID is 5 6 6 7 so just 2 5 6 so 6 7 and press enter and you can see here this signal is received here it says file deleted ok so the signal is trapped here and then let's see if the file is deleted or not yes file is also deleted ok so you can execute any command using trap when some signal is received ok now let's run the script once again and let me show you one more thing so script is running and then I just to trap here and then press enter and I can see here that there are some traps or there is a list of some traps right so this is the list from these three traps are from this script and this these three traps are from some other trap command I have given in the terminal right so previously I have given a trap command to test this trap and this is from the crack command in the terminal previously I have given and these three values are coming from this script okay now the question is how we can remove traps okay because we have set some trap and whenever we use trap c'mon they are listed right so somehow we want to remove these traps also so how do we remove it so we can just remove the trap by using - and name of the signal so I want to remove for example these three traps then I can just do trap - and then these three signals whatever trap I want to remove so I do this and once again trap and then I only see these traps now okay so these three traps I have removed right so this is how also you can remove traps so this - is for removing okay so this is how you can use traps in shell scripting I hope you enjoyed this blog

Post a Comment

Previous Post Next Post

Recent in Technology News