LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-21-2006, 03:02 PM   #1
basak
Member
 
Registered: Jul 2006
Posts: 41

Rep: Reputation: 15
kill-trap


hi

In the "traptest" script below I want the script say "bye" and exit if it gets a SIGHUP signal.
In the "mycommand" script which is called from "traptest" I send the SIGHUP signal.

#!/bin/bash
#mycommand

calledprg=
while [ "$1" != "" ]; do
case $1 in
-s ) shift
calledprg=$1
;;
* ) exit 1

esac
shift
done

if [ "$calledprg" != "" ];then
pkill -s 1 $calledprg
fi
exit

#!/bin/bash
#traptest

PROGNAME=$(basename $0)
echo "$PROGNAME"
trap " echo "byeeeeeeeeeee" ; exit " SIGHUP


mycommand -s $PROGNAME
while :
do
echo "sleeeeppppp"
sleep 60
done




but I get

line 5: trap: have: invalid signal specification
line 5: trap: finished ; exit : invalid signal specification

what is wrong with these 2 scripts?

Also in some tutorials
they say SIGKILL should not be used if not urgent.Why?Instead of SIGKILL what would be used that makes sure that the program will be killed in any case?(in another script I used pkill -s 9 but I am not confident because of these comments)

Thank u for reading all theseand thank u much more if u have any idea
 
Old 08-21-2006, 03:56 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,893

Rep: Reputation: 339Reputation: 339Reputation: 339Reputation: 339
Change the line:

trap " echo "byeeeeeeeeeee" ; exit " SIGHUP

to:

trap ' echo "byeeeeeeeeeee" ; exit ' SIGHUP

--------------------
Steve Stites
 
Old 08-21-2006, 04:46 PM   #3
basak
Member
 
Registered: Jul 2006
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks ,but there is still the problem that "mycommand" does not make "traptest" exit
 
Old 08-21-2006, 06:48 PM   #4
basak
Member
 
Registered: Jul 2006
Posts: 41

Original Poster
Rep: Reputation: 15
Code:
#!/bin/bash
#traptest

PROGNAME=$(basename $0)
echo "$PROGNAME"
trap ' echo "byeeeeeeeeeee" ; exit ' SIGHUP


mycommand -s $PROGNAME
while :
do
echo "sleeeeppppp"
sleep 60
done


Code:
#!/bin/bash
#mycommand

calledprg=
while [ "$1" != "" ]; do
case $1 in
-s ) shift
calledprg=$1
;;
* ) exit 1

esac
shift
done

#--------------------------
# some other work done here
# 
#--------------------------
if [ "$calledprg" != "" ];then
pkill -s 1 $calledprg   ##sends the SIGHUP signal to traptest
fi
exit


that looked better I guess
Traptest never gets out of the while loop even though it should exit when "mycommand" sends it the SIGHUP signal..
 
Old 08-21-2006, 08:05 PM   #5
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,893

Rep: Reputation: 339Reputation: 339Reputation: 339Reputation: 339
I followed your logic through to this line:

pkill -s 1 $calledprg ##sends the SIGHUP signal to traptest

In that line the value of $calledprg is "bash" so you are not trying to pkill traptest, you are trying to pkill bash. You are sending SIGHUP to bash and bash is ignoring you.

I suggest that you forget about basename and use pid. Have traptest send its pid to mycommand and have mycommand pkill the ppid.

-----------------------
Steve Stites
 
Old 08-22-2006, 05:05 AM   #6
basak
Member
 
Registered: Jul 2006
Posts: 41

Original Poster
Rep: Reputation: 15
I am sorry but passing the PID does not solve the problem..Can the problem be due to the fact that mycommand runs in a subshell when it is called ,and passing traptest's PID or name is useless because in the subshell shell in which mycommand runs there is no such running program to kill ...???????????
 
Old 08-22-2006, 09:41 AM   #7
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,893

Rep: Reputation: 339Reputation: 339Reputation: 339Reputation: 339
"I am sorry but passing the PID does not solve the problem."

Could you post the code where you passed the pid and it didn't work?

-------------------
Steve Stites
 
Old 08-23-2006, 10:10 AM   #8
basak
Member
 
Registered: Jul 2006
Posts: 41

Original Poster
Rep: Reputation: 15
Code:
#!/bin/bash
#traptest

trap " echo "byeeeeeeeeeee" ; exit " SIGHUP

mypid=$(ps | grep traptest |awk '{print $1}')
mycommand -s $mypid
while :
do
echo "sleeeeppppp"
sleep 5
done
Code:
#!/bin/bash
#mycommand

calledprg=
while [ "$1" != "" ]; do
             case $1 in
             -s ) shift
                  calledprg=$1
                  ;;
              * ) exit 1

   esac
   shift
   done

if [ "$calledprg" != "" ];then
kill -SIGHUP $calledprg
fi
exit
thank u for your help jailbait
Also can the problem be that "mycommand" sends the signal to "traptest" when its in sleeping mode and its pid is not the same any more???(I could not figure out why this signal handling does not workI have been trying everything during these last two days,but I have nothing in the end((
 
Old 08-23-2006, 10:12 AM   #9
basak
Member
 
Registered: Jul 2006
Posts: 41

Original Poster
Rep: Reputation: 15
+
I could not use the "pidof" command due to system settings,it is not available to my bash shell..
 
Old 08-23-2006, 03:02 PM   #10
basak
Member
 
Registered: Jul 2006
Posts: 41

Original Poster
Rep: Reputation: 15
 
Old 08-23-2006, 05:38 PM   #11
basak
Member
 
Registered: Jul 2006
Posts: 41

Original Poster
Rep: Reputation: 15
can the problem be due to the fact that

Note that while a parent process gets the process ID of the child process, and can thus pass arguments to it, the reverse is not true. This can create problems that are subtle and hard to track down.

as written in

http://elibrary.fultus.com/technical.../internal.html

that "mycommand" is a child process and parent process "tarptest" can not pass its PID to a child process????????
 
  


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
how to send snmp trap & recieve trap in C program minil Programming 3 07-10-2010 09:22 AM
Kernel trap (Fatal trap 12) m!k@EL *BSD 4 09-05-2007 11:58 PM
What is trap program?? Zero-0-Effect Linux From Scratch 2 09-17-2004 09:07 PM
trap command for c? onnyloh Programming 3 09-13-2004 04:06 AM
setting a trap antken Linux - Security 6 01-11-2003 06:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:28 AM.

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
Open Source Consulting | Domain Registration