HomeОбразованиеRelated VideosMore From: theurbanpenguin

BASH scripting lesson 8 using TRAP to control scripts

96 ratings | 12228 views
More videos like this online at http://www.theurbanpenguin.com Now we really have some fun with our scripts adding in TRAP to help make the scripts more robust. We may need our script to exit correctly each time. We can do this by creating a PID file and checking for its existence. If it does exist then the script did not exit cleanly last time. we also then must make sure we provide a method to delete the file as we exit. Scripts normally do not respond to terminate signals sent from kill, but we too can eable this with traps to make sure we listen to calls from the system, I am sure this will help with your scripts and you will want to implement these ideas in some of your projects.
Html code for embedding videos on your blog
Text Comments (12)
Benjamin Shtark (3 months ago)
Hi, A question, what would you do if you start a program in a subprocess (background), like a java application via your script and you still need to have PID files to protect your startup script from launching multiple java background apps and delete that .pid file when java app exits?
Sanjay Kumar (2 years ago)
Very nice explanation... Thank you !!!
Kevin H (2 years ago)
Hello , why you use double parentheses or nest parentheses (( count < 10 )) and (( count ++ ))? is ( count < 10 ) wrong?
Kevin H (2 years ago)
thank you :)
theurbanpenguin (2 years ago)
+Kevin H (( )) is for mathematical calculations in BASH and yes ( count < 10 ) does not run the test. If you did not want you use the double brackets then is would be [ $count -lt 10 ]
MArius Murzea (3 years ago)
What key did you press to exit from bash code??
Dawid Piotrowski (2 years ago)
+MArius Murzea Do you mean exiting vi? You normally exit vi (and vim) by going to the command mode (pressing ESC) and then typing ":q" (without quotes, obviously). If you want to write changes to the file before exiting you write ":w", then ":q", or just use both together ":wq".
Samant Rao (2 years ago)
+MArius Murzea Can you be a bit more specific? may be I can help you.
Rui Xue (3 years ago)
Cool! Learnt more and indeed understand a bit deeper on Linux OS <3
Starcom23 (4 years ago)
Starcom23 (4 years ago)
Why behind trap statement you have EXIT in capital letters, while in loop exit is in small? is it always like that?
theurbanpenguin (4 years ago)
EXIT in the trap is a keyword as opposed to the command exit in lowercase.

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.