Ctrl-C or Ctrl-Z causing exit the session


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ctrl-C or Ctrl-Z causing exit the session
# 1  
Old 06-29-2010
Ctrl-C or Ctrl-Z causing exit the session

H! I have written script where it need to invoke the perl script in background, then write the pid in temp file then bring back the job to foreground. whenever the Ctrl-C or Ctrl-Z is pressed in the script has to exit and prompt should be dispalyed. but this script causing exit from shell session itself if press Ctrl-C and I tried even after setting stty intr=^c also stty -a is fine. I want to trap both Ctrl-C or Ctrl-Z and it should not exit the session but only script should exit, not login shell.

Did you find any code problem? or what might be the problem? also below fg statement echoing the commond how can we avoid that. I used 2>/dev/null, till it not working.

Please help me in this.

I have written the code as below:
Code:
#!/bin/sh
 cleanup()
 {
   echo "Caught Signal ... Terminated."
   exit 1
 }
        ...       
                  
        while [ 1 ]
 do
 # enter the optional parameter if we need to
         set -m
         #invoke perl script in background
         ${HOME}/${PROCESS}.pl ${OPTIONAL} 2>&1 > $LOGFILENAME &
         #check exit status
         status=$?
         if [ $status -ne 0 ]
         then
                 echo ${PROCESS}.pl ${OPTIONAL} Job fails with status $status
                 exit $status
         fi
         # write the PID into files
         echo $! > ${PID_DIR}/${PROCESS}.perl
         chmod 755 ${PID_DIR}/${PROCESS}.perl
         
         # then bring the job to foreground
         fg %+ 2>/dev/null
         trap 'cleanup' 2 9 15 17 19 20
         sleep ${SLEEP};
 done
 exit 0

Really appeciated if any suggestion.

Last edited by pludi; 06-29-2010 at 09:19 AM.. Reason: code tags, please...
# 2  
Old 06-29-2010
There are a few problems in the script.

Quote:
${HOME}/${PROCESS}.pl ${OPTIONAL} 2>&1 > $LOGFILENAME &
#check exit status
status=$?
The value of $status is not the exit status of the Perl program, it is the exit status of "&" which would normally be zero. This affects the whole logic of your script.
Backgrounding the Perl program seems pointless because it is immediately brought to foreground.

Quote:
trap 'cleanup' 2 9 15 17 19 20
To be effective the trap needs to be before the "while" line.


The script as it stands will try to start an infinite number of identical Perl programs unless the trap is triggered when it will execute "cleanup" and leave the Perl program(s) running. If the value of $SLEEP} is greater than the run time of one Perl program it may only run one Perl program at a time.

There is no provision for stopping the Perl program or waiting for it to finish before starting another one. It is unclear whether you want to stop the Perl program when someone presses ctrl/c or ctrl/z . Maybe I'm being thick but I don't understand what you are trying to do.
# 3  
Old 06-30-2010
Thanks a lot for you reply.

Actually this script was already written long back in sunsolaris and I need to migrate it to GNU linux. the exact code is as below

code:
Code:
while [ 1 ]
do # enter the optional parameter if we need to set -m ${HOME}/${PROCESS}.pl ${OPTIONAL} 2>&1 > $LOGFILENAME & #check exit status status=$? if [ $status -ne 0 ] then echo ${PROCESS}.pl ${OPTIONAL} Job fails with status $status exit $status fi echo $! > ${PID_DIR}/${PROCESS}.perl chmod 755 ${PID_DIR}/${PROCESS}.perl cat ${PID_DIR}/${PROCESS}.perl | ${MAIL_BIN} -s "\"${MAIL_HEADING1}\"" ${MAIL_LIST} fg %+ cat ${PID_DIR}/${PROCESS}.perl | ${MAIL_BIN} -s "\"${MAIL_HEADING2}\"" ${MAIL_LIST} sleep ${SLEEP}; done
exit 0


1) the main aim of running the perl script in background, is to send PERL script PID to mail when it started.
2) Once the script execution is competed then again script need to notify the finish.
3) here the SLEEP is set to 2
4) When I stared the script its

Code:
> ./script.sh.new new Enable &
[1] 5932
> ${HOME}/${PROCESS}.pl ${OPTIONAL} 2>&1 >$LOGFILENAME

4) when press Ctrl-Z:
Code:
> ${HOME}/${PROCESS}.pl ${OPTIONAL} 2>&1 >$LOGFILENAME
exit
>

* echoing the exit and exiting the current shell but these perl and shell script are running in background. Same case even for Ctrl-C

I want to exit the PERL and SHELL when I pressed the Ctrl-C or Ctrl-Z. Please suggest or send me some sample script to handle these.



Last edited by jramesh1; 06-30-2010 at 03:50 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automation of keyboard inputs..like Ctrl+d and Ctrl+a

Hi..! I'm stuck with my automation of starting a process and keeping it running even after the current ssh session has exited.. So i'm trying to use command 'screen'. which is doing exactly what i wanted, But the problem is automation of the same. i will have to press Ctrl+a and Ctrl+d for... (2 Replies)
Discussion started by: chandana hs
2 Replies

2. UNIX for Dummies Questions & Answers

Ctrl-V + Ctrl-J for newline character does not work inside vi editor

Hi friends, I am trying to add a newline char ('\n') between the query and the commit statement in the following shell script. #! /bin/sh echo "select * from tab; commit;" > data.sql I have tried typing in "Ctrl-V + Ctrl-J" combination which has inserted ^@ (NUL) character but the commit... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. Shell Programming and Scripting

I dont want to exit the program by ctrl + c

Hey , guys I am new to shell programing ,, so need a help from you guys ... I have to write a shell script that accepts file name or directory name from the user if it is a directory then throw an error if it is a file then give the user two options . 1.overwrite the content 2.append the... (2 Replies)
Discussion started by: coolashu
2 Replies

4. Shell Programming and Scripting

How to handle CTRL+Z or CTRL+C in shells script?

Hi, while executing shell script, in the middle of the process, if we kill the shell script( ctrl+z or ctrl+c), script will be killed and the files which using for the script will be in the folder. How to handle those scenarios. Is there any possibilities, if user breaks the script, I need to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

5. Linux

ctrl+c not working

Hi All, I have ran one command tail -f <filename> to view newly appended text to that file. When i was done i pressed ctrl+c command to stop it but to my suprise it didn't work. I then tried top command and pressed ctrl+c and it worked fine and command aborted. I then just checked the tail... (0 Replies)
Discussion started by: tushar_shah06
0 Replies

6. Shell Programming and Scripting

ctrl-c in script

how to send ctrl-c signal in scripts ??? i am executing top command in .exp script top command gives cpu running activities on your machine eg # top top - 18:41:01 up 8:38, 5 users, load average: 0.03, 0.16, 0.16 Tasks: 172 total, 1 running, 170 sleeping, 0 stopped, 1 zombie... (3 Replies)
Discussion started by: alexzander18
3 Replies

7. Programming

Catch ctrl+d in C

Hello, I am programming some kind of shell in special distribution of Linux. My trouble comes with programming the sort function. It should work the same like in the standard shell. When you terminate input, there's need to put End Of Transmission character, which is CTRL+D. But I am not able to... (2 Replies)
Discussion started by: samciz
2 Replies

8. Shell Programming and Scripting

Catching ctrl-C or ctrl-D

Hi there, I'm using HP-UX 11 machine. I am running a script, thats gonna take a long time to execute. When I press ctrl-c to come out of my script, I have to catch that signal(ctrl-c) and display that ctrl-c had been pressed. How can I do it. Thanks in advance (2 Replies)
Discussion started by: sendhilmani123
2 Replies

9. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies

10. UNIX for Dummies Questions & Answers

Enabling Ctrl-c

Do you have to set somthing in a users .profile to allow use of Ctrl-C (3 Replies)
Discussion started by: Corewin
3 Replies
Login or Register to Ask a Question