I have a very simple script to trap signals:  #!/bin/bash                                                                      unlink SigTermRecieved unlink SigIntRecieved #trap " touch SigTermRecieved ; exit " SIGTERM                                   trap " touch SigIntRecieved ; exit " SIGINT echo "pid is $$" while true do     sleep 60m done exit   BEHAVIOR                                                                        with both trap lines commented:   SIGTERM sent: terminates program. ? SIGINT sent from another terminal window is IGNORED!   CTRL C terminates program.                                                     with SIGTERM trap uncommented: ? SIGTERM sent: program does not terminate and SIGTERM trap is not executed. ? SIGINT sent from another terminal window is IGNORED!   CTRL C terminates program.                                                     with SIGINT trap uncommented:   SIGTERM sent: terminates program. ? SIGINT from another terminal window is IGNORED!   CTRL C executes trap code and terminates program.                              The behaviors preceded by ? are unexpected or wrong. I'm not too concerned about the SIGINT behaviors. I tried those to see if any traps would work.  So, I removed the snooze function and just have:  sleep 60m &amp; wait $!   Now, kill -TERM -pid kills both processes and executes the trap for SIGTERM.  In my application, I'm waiting for SIGTERM from system shutdown, so I can clean up some details. I don't know whether system shutdown uses pid or -pid, both work for me, and the system will eventually kill all the processes. A few dangling sleep processes while I'm testing is no big deal.  That fifo solution is quite complicated and I don't understand it.  Thanks for the help.
121907
I've got the following script:  #!/bin/bash echo "We are $$" trap "echo HUP" SIGHUP cat    # wait indefinitely   When I send SIGHUP (using kill -HUP pid), nothing happens.  If I change the script slightly:  #!/bin/bash echo "We are $$" trap "kill -- -$BASHPID" EXIT    # add this trap "echo HUP" SIGHUP cat    # wait indefinitely   ...then the script does the echo HUP thing right as it exits (when I press Ctrl+C):  roger@roger-pc:~ $ ./hupper.sh  We are 6233 ^CHUP   What's going on? How should I send a signal (it doesn't necessarily have to be SIGHUP) to this script?
123988
I have a program which produces useful information on stdout but also reads from stdin. I want to redirect its standard output to a file without providing anything on standard input. So far, so good: I can do:  program &gt; output   and don't do anything in the tty.  However, the problem is I want to do this in the background. If I do:  program &gt; output &amp;   the program will get suspended ("suspended (tty input)").  If I do:  program &lt; /dev/null &gt; output &amp;   the program terminates immediately because it reaches EOF.  It seems that what I need is to pipe into program something which does not do anything for an indefinite amount of time and does not read stdin. The following approaches work:  while true; do sleep 100; done | program &gt; output &amp; mkfifo fifo &amp;&amp; cat fifo | program &gt; output &amp; tail -f /dev/null | program &gt; output &amp;   However, this is all very ugly. There has to be an elegant way, using standard Unix utilities, to "do nothing, indefinitely" (to paraphrase man true). How could I achieve this? (My main criteria for elegance here: no temporary files; no busy-waiting or periodic wakeups; no exotic utilities; as short as possible.)
59914
I want to start a process from a shell script, allow it to interact with the user (so I cannot just background it, grab the PID, launch sleep followed by sanity checks and kill), but if it is still running after a set period of time (say, two minutes), then and only then do I want to kill it.  The intention is to simply implement a wallclock-run-time timeout for a program that doesn't have that capability built in.  Is there a way to do this, preferably from a bash shell script?
109593
Let's assume process runs in ulimited environment :  ( ulimit  ... -v ... -t ... -x 0 ... ./program )   Program is terminated.  There might be many reasons : memory/time/file limit exceeded ; just simple segfault ; or even normal termination with return code 0.  How to check what was the reason of program termination, without modifying program?  P.S. I mean "when binary is given". Maybe some wrapper (ptrace-ing etc) might help?
26012
Transmission is intermittently hanging on my NAS.  If I send SIGTERM, it doesn't disappear from the process list and a &lt;defunct&gt; label appears next to it.  If I send a SIGKILL, it still doesn't disappear and I can't terminate the parent because the parent is init.  The only way I can get rid of the process and restart Transmission is to reboot.  I realize the best thing I can do is try and fix Transmission (and I've tried), but I'm a novice at compiling and I wanted to make sure my torrents finished before I start messing around with it.
2844
I'm working on a Linux machine through SSH (Putty). I need to leave a process running during the night, so I thought I could do that by starting the process in background (with an ampersand at the end of the command) and redirecting stdout to a file.  To my surprise, that doesn't work. As soon as I close the Putty window, the process is stopped.  How can I prevent that from happening??
93630
Suppose I have a process which spawns exactly one child process. Now when the parent process exits for whatever reason (normally or abnormally, by kill, ^C, assert failure or anything else) I want the child process to die. How to do that correctly?    Some similar question on stackoverflow:   (asked earlier)  (asked later)      Some similar question on stackoverflow for Windows:
21040
I just wonder why kill -9 0 ends my tty console session?   What exactly is PID 0, it is not listed on a ps aux?
56379
I'm writing a wrapper class for a command line executable.  This exe accepts input from stdin until I hit Ctrl+C in the command prompt shell, in which case it prints output to stdout based on the input.  I want to simulate that Ctrl+C press in C# code, sending the kill command to a .NET Process object.  I've tried calling Process.Kill(), but that doesn't seem to give me anything in the process's StandardOutput StreamReader.  Might there be anything I'm not doing right?  Here's the code I'm trying to use:  ProcessStartInfo info = new ProcessStartInfo(exe, args); info.RedirectStandardError = true; info.RedirectStandardInput = true; info.RedirectStandardOutput = true; info.UseShellExecute = false; Process p = Process.Start(info);  p.StandardInput.AutoFlush = true; p.StandardInput.WriteLine(scriptcode);  p.Kill();  string error = p.StandardError.ReadToEnd(); if (!String.IsNullOrEmpty(error))  {     throw new Exception(error); } string output = p.StandardOutput.ReadToEnd();   The output is always empty, even though I get data back from stdout when I run the exe manually.  Edit: This is C# 2.0 by the way.
72553
I am running the script below to automatically download files from a server using lftp. It works except that when it runs I get the error message   trap: SIGINT: bad trap   If I replace SIGINT and SIGTERM with INT and TERM then it does work, but I don't know if it then achieves the same purpose. This is on Linux Debian 4.9.2-10.  #!/bin/sh login="login" pass="password" host="server.server.com" remote_dir='~/remote/dir' local_dir="/local/dir"  base_name="$(basename "$0")" lock_file="/tmp/$base_name.lock" trap "rm -f $lock_file" SIGINT SIGTERM if [ -e "$lock_file" ] then     echo "$base_name is running already."     exit else     touch "$lock_file"     /usr/bin/lftp -p 22 -u "$login","$pass" sftp://"$host" &lt;&lt; EOF     set sftp:auto-confirm yes     set mirror:use-pget-n 5     mirror -c -P5 "$remote_dir" "$local_dir"     quit EOF     rm -f "$lock_file"     trap - SIGINT SIGTERM     exit fi
49657
Comparing bash, ash and dash, only bash can be terminated by SIGTERM  kill -TERM &lt;pid&gt;   For ash and bash, I need SIGHUP or SIGKILL  kill -HUP &lt;pid&gt; kill -KILL &lt;pid&gt;   What's the reasoning behind this? Furthermore, the behavior seems to be system-dependent:   bash is terminated by SIGTERM on Ubuntu 14.04 Trusty, Ubuntu 16.04 Xenial, Ubuntu 18.04 Bionic, Ubuntu 19.10 Eoan, Ubuntu 20.04 Focal, Debian 8 Jessie, Debian 9 Stretch, Debian 10 Buster, CentOS 8 bash is not terminated by SIGTERM on CentOS 6, CentOS 7
83707
I'm not sure what how or why this happens but sometimes when I open programs through terminal the terminal continues to attach itself to the program. I can understand this for things like "sudo nautilus" but for regular programs this is extremely annoying.   I cannot detach terminal from program because if I ctrl+c then it closes the program and if I quit the terminal then the program closes as well.  Few questions:   Why does this happen? How do I manually activate/deactivate this function? How do I detach the terminal from the program?   If I am asking too much then I understand. In that case I don't even know how to google the answer so if you can point me to any resources that talks about this or give me the name of the function that is happening that that would be great.   Thanks.
34264
I'm using tomcat, and sometimes when I tell it to stop it doesn't properly kill the process.  My way around this is too do:  lsof -i tcp:8080   which outputs:  COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME java    888 root   35u  IPv6 780659      0t0  TCP *:http-alt (LISTEN) java    888 root   39r  IPv6 790103      0t0  TCP localhost:58916-&gt;localhost:http-alt (CLOSE_WAIT) java    888 root   40r  IPv6 792585      0t0  TCP localhost:58936-&gt;localhost:http-alt (CLOSE_WAIT) java    888 root   75r  IPv6 785553      0t0  TCP localhost:58701-&gt;localhost:http-alt (CLOSE_WAIT) java    888 root   77r  IPv6 787642      0t0  TCP localhost:58814-&gt;localhost:http-alt (CLOSE_WAIT) java    888 root  130u  IPv6 783894      0t0  TCP localhost:58686-&gt;localhost:http-alt (CLOSE_WAIT) java    888 root  353u  IPv6 780929      0t0  TCP localhost:58632-&gt;localhost:http-alt (CLOSE_WAIT)   I then run   kill -9 pid   I want a way to get all the pid numbers and kill them.  Thing is I don't know how to isolate that field.
115867
I am completely new to *NIX based OSes. One of the things that baffles me is that a process or program may execute setuid(0) and then perform privileged operations and revert back to its normal uid.  My question is what is the mechanism in *NIX to prevent any arbitrary process from possessing root ?  If I write a simple C program that calls setuid(0) under what conditions will that call succeed and under what conditions will it fail ?
95920
Let's say I have a bash script with the following:  #!/bin/sh gedit rm *.temp   When I execute it using sh ./test.sh, gedit pops-up but the rm part does not run until after I close gedit.  I want the script to continue running even if gedit isn't closed; like the gedit isn't blocking the bash execution.  The example I gave is just an example (putting the rm first won't work in a real situation).
97275
I define a shell keybinding in my ~/.bashrc like so:  bind -x '"\e\C-w":"/usr/bin/reset"'   This does make Ctrl-Alt-w launch the reset executable. However, it leaves the terminal in a state that resembles the one you get into by pressing Ctrl-v. So, for example, if immediately after resetting in this way I press Ctrl-p, instead of invoking the action of that shortcut (repeat previous command) I get ^P printed.  Any idea what's causing this problem and how to fix it?  EDIT: I am attaching a Python program that reproduces the messed-up terminal state I am trying to fix with the reset. I encounter this condition very often while debugging multi-threaded applications:  import pdb, thread, time  def interrupt():     time.sleep(2)     pdb.set_trace()  thread.start_new_thread(interrupt, ()) raw_input('? ')   To reproduce the messed-up state, just run the code above, and press Enter when you see the debugger invoked (it will print a bunch of text, ending with pdb.set_trace(), about two seconds after you run the program and the initial prompt appears).
83138
If I issue the trap builtin twice [for the same signal], what happens? Is the second command added to the first, or does it replace the first?  trap Foo SIGINT ... trap Bar SIGINT ...   When SIGINT happens, does Bash just run Bar, or does it run Foo as well? Or something else...?