LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2016, 12:53 PM   #1
mikenash
Member
 
Registered: Dec 2014
Posts: 84

Rep: Reputation: Disabled
Trap command not working


Working with the trap command to delete a file. I reduce the script as a simple example. If I allow the script to time out the file will be erased. If I kill the process with -9 or -15 the file will not be erased. Any suggestions?
Code:
#!/bin/bash
#store the pid of this script into a temp file
echo $$ > testtrap_pid
trap "rm -f testtrap_pid ; exit" SIGTERM SIGKILL EXIT
sleep 60

sh testtrap.sh&
[2] 2994

ls
anaconda-ks.cfg  Documents    install.log.syslog       michael   Public        testtrap.sh
autorestart_pid  Downloads    LinuxOne05092016.tar     Music     Templates     Videos
Desktop          install.log  LinuxOne05092016www.tar  Pictures  testtrap_pid  vncrestart.sh

>kill -9 2994
[2]+  Killed                  sh testtrap.sh

ls
anaconda-ks.cfg  Documents    install.log.syslog       michael   Public        testtrap.sh
autorestart_pid  Downloads    LinuxOne05092016.tar     Music     Templates     Videos
Desktop          install.log  LinuxOne05092016www.tar  Pictures  testtrap_pid  vncrestart.sh
 
Old 05-12-2016, 01:46 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,198

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
It is without the SIG prefix:
Code:
trap "rm -f testtrap_pid ; exit" TERM INT HUP EXIT
The brutal SIGKILL is handled by the kernel and cannot be captured.

Note that "testtrap_pid" is in the current directory. That means you may not use a cd command in the shell code!
An absolute path is more robust
Code:
pidfile=/path/to/testtrap_pid
trap "rm -f $pidfile" TERM INT HUP EXIT
 
1 members found this post helpful.
Old 05-13-2016, 07:31 AM   #3
mikenash
Member
 
Registered: Dec 2014
Posts: 84

Original Poster
Rep: Reputation: Disabled
Thank you for the information in your reply. I appreciate it very much. All is working well, except for the SIGKILL.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trap command waddles Programming 6 11-14-2013 01:22 PM
How to use trap command with a function notepod Linux - Software 12 09-02-2009 02:14 AM
Regarding trap command sharp859 Linux - Newbie 1 05-11-2009 07:35 PM
what does the command do: trap ' ' 1 2 ? manohare Linux - General 2 03-02-2009 05:51 AM
trap command for c? onnyloh Programming 3 09-13-2004 04:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration