cannot exit terminal with user privileges after running script

Discussion in 'Command Line' started by emekadavid, Aug 5, 2012.

  1. emekadavid

    emekadavid Member Staff Writer

    Messages:
    63
    Likes Received:
    5
    Trophy Points:
    8
    Please can anyone help explain what this means?
    When I entered the command exit on my user terminal, I got the following report:
    Code:
    $ exit
    exit
    There are no stopped jobs.


    I then ran the jobs command. It gave me this listing.
    Code:
    [1]   Stopped                 ./bells
    [2]   Stopped                 ./bells
    [3]   Stopped                 ./bells
    [4]   Stopped                 ./bells
    [5]   Stopped                 ./bells
    [6]   Stopped                 ./bells
    [7]   Stopped                 ./bells
    [8]   Stopped                 ./bells_copy
    [9]-  Stopped                 ./bells_copy
    [10]+  Stopped                 ./bells_copy
    
    And when I ran the ps command I received this other listing. 
      PID TTY          TIME CMD
     1950 pts/1    00:00:00 bash
     2110 pts/1    00:00:00 bells
     2111 pts/1    00:00:00 timeout <defunct>
     2113 pts/1    00:00:00 bells
     2114 pts/1    00:00:00 timeout <defunct>
     2143 pts/1    00:00:00 bells
     2144 pts/1    00:00:00 timeout <defunct>
     2186 pts/1    00:00:00 bells
     2187 pts/1    00:00:00 timeout <defunct>
     2193 pts/1    00:00:00 bells
     2194 pts/1    00:00:00 timeout <defunct>
     2208 pts/1    00:00:00 bells
     2209 pts/1    00:00:00 timeout <defunct>
     2214 pts/1    00:00:00 bells
     2215 pts/1    00:00:00 timeout <defunct>
    10947 pts/1    00:00:00 bells_copy
    10951 pts/1    00:00:00 bells_copy
    10962 pts/1    00:00:00 bells_copy
    11067 pts/1    00:00:00 ps
    
    The bells and bells_copy are scripts which I wrote. some were stopped with Ctrl-Z while others were allowed to run normally.
    timeout is a command that is executed from within a function in both scripts and at Ctrl-Z, the timeout command is killed using "killall timeout". Can anyone explain why the exit command refuses to exit the terminal?
    I tried killing the processes individually using their <pid>s but ps gives the same report as jobs i.e no change.
    How do i go about trashing the terminal successfully with exit?
    tnx
  2. ehansen

    ehansen New Member Staff Writer

    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Well, first, Ctrl+Z doesn't terminate processes, it suspends them to the background. Also, running the same script numerous times can cause a hiccup, but since we don't know what said scripts do and what their code looks like it's hard to tell.

    Assuming bells & bells_copy deal with I/O (i.e.: read/write from/to the hard disk), it could be causing the issue at hand. What command exactly do you run to kill the processes? If you use "kill <pid>" then it won't do much of anything. By default, kill uses the signal 15 which tells the process "hey, when you get done running, or at your earliest convenience, please end". However, if you use signal 9 (kill -9 <pid>), it says "<pid>, I don't care what you're doing, terminate now!"

    If you really don't want to investigate this issue, however, just run "killall <process name>" and it'll kill all instances of that process.
    Rob likes this.
  3. Rob

    Rob Administrator Staff Member

    Messages:
    573
    Likes Received:
    185
    Trophy Points:
    43
    How'd this work out for you emekadavid?
  4. emekadavid

    emekadavid Member Staff Writer

    Messages:
    63
    Likes Received:
    5
    Trophy Points:
    8
    I used "killall <processname>" and in the script logic, when the script catches a trap, either STOP, TERMINATE or INTERRUPT, it enters a function where "killall <processname>" is run. maybe i will run a test again and see how the "jobs" and "ps" command will report when the script is allowed to run to completion and when interrupted with Ctrl-Z.
    will give you feedback later. tnx
  5. emekadavid

    emekadavid Member Staff Writer

    Messages:
    63
    Likes Received:
    5
    Trophy Points:
    8
    re: cannot exit terminal with user priv

    I think the problem lies with the script logic. Can you help me out.
    I ran the script and then allowed it to exit normally, without any interrupts. When I checked the "ps" and "jobs" command, I got the expected result.
    Then i ran the script and interrupted it. When i ran the "ps" command this is the result:
    Code:
      PID TTY          TIME CMD
     1911 pts/0    00:00:00 su
     1914 pts/0    00:00:00 bash
     2058 pts/0    00:00:00 bells2
     2059 pts/0    00:00:00 timeout <defunct>
     2081 pts/0    00:00:00 ps
    
    
    And when I ran "jobs" this is the report received.
    Code:
    [1]+  Stopped                 ./bells2
    
    Ctrl-Z should have killed the timeout process but it didn't.
    I am attaching a copy of the script for your perusal.
    I think there is a consequence to this report, not only that it stops the terminal with user privileges from exiting. There can be other consequences.
    Thanks for your kind attention. I do appreciate your response.
    david

    Attached Files:

  6. ehansen

    ehansen New Member Staff Writer

    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Some things to note:

    1) Ctrl+Z is not an interrupt. It simply moves the process to the background so you can continue working on things while said process continues running.

    2) Ctrl+Z does NOT kill a process. Read #1 to understand why.

    3) This line looks like it has an error: "timeout "$duration"m top;". First, then '"m' part. Then the "top;".
  7. emekadavid

    emekadavid Member Staff Writer

    Messages:
    63
    Likes Received:
    5
    Trophy Points:
    8
    regards #1: thanks for the insight. I thought Ctrl-Z kills processes. that explains my confusion. but Ctrl-z did not kill the process itself. how would you kill a script that is running and is placed on a timer if ctrl-c doesn't kill it?
    regards #3: that line has no error. $duration expands to the duration the user inputted and the m is the suffix that gives the duration in minutes as distinct from 's' for seconds and 'h' for hours. as for top, i intended using : command for the commands part of the timeout command (read the man) but bash did not accept :, so i used a command that should last longer than the input $duration.
    thanks. again, if ctrl-c doesn't kill a process hung on a timer, what will?
  8. ehansen

    ehansen New Member Staff Writer

    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    It's been a few months since I did work in Bash, heh.

    For killing processes, assuming you haven't ran any other commands since running bells.sh, you can do kill -9 $! which expands to "kill -9 <pid of last ran command>". However, I really think you should better debug your code in the fact of taking it apart piece by piece, and seeing where and why it hangs.
  9. emekadavid

    emekadavid Member Staff Writer

    Messages:
    63
    Likes Received:
    5
    Trophy Points:
    8
    i was testing the trap mechanism on the script, i.e ctrl-c, which did not work as plotted. but ctrl-z sent the script execution to the background. i think the code is okay; from the threads so far, just i didn't understand why it didn't catch ctrl-c.
    will keep wondering.
  10. Kerms

    Kerms New Member

    Messages:
    59
    Likes Received:
    12
    Trophy Points:
    0
    ctrl+c sends the SIGINT signal to the process, which then I guess the script forwards the signal to the currently running command in the script. Sending SIGINT is like asking the process to die. However if the process was suspended (ctrl+z, SIGTSTP), or the process was programmed to deal with SIGINT other than the default way (to terminate), the process doesn't die if ctrl+c is given. You can send the SIGQUIT signal usually via ctrl+\ which most of the time when ctrl+c doesn't work, this works.
    emekadavid likes this.
  11. emekadavid

    emekadavid Member Staff Writer

    Messages:
    63
    Likes Received:
    5
    Trophy Points:
    8
    referencing this. lol
  12. emekadavid

    emekadavid Member Staff Writer

    Messages:
    63
    Likes Received:
    5
    Trophy Points:
    8
    Thanks Kerms. ctrl+\ worked. thanks a lot. lol
    david

Share This Page