I have scriptA which will execute another script which will startup in the background. Now I need to make sure that when I kill scriptA (cmd+c) that the background processes are also killed.   #!/bin/bash echo "This script is about to run another script."   sh ../processes/processb/bin/startserver.sh &amp;  FOO_PID=$!  echo "This script has just run another script." $FOO_PID   This script executes fine, but once I press cmd+c and do a 'ps' command on FOO_PID value , that process still exists. What am I doing wrong?  UPDATE-----------  So I tried out below code, but still scriptC's process is not getting killed. I think it just only terminates scriptA ( parent) when pressed ctrl+c and therefore trap command does not get executed?       #!/bin/bash  echo "This script is about to run another script."     ../common/samples/bin/scriptC.sh &amp;  mypid=$! kill -0 "$mypid" &amp;&amp; echo "My process is still alive."  echo "This script has just run another script." $mypid  trap "kill $mypid &amp;&amp; kill $$" INT
11663
I am looking for a way to clean up the mess when my top-level script exits.  Especially if I want to use set -e, I wish the background process would die when the script exits.
27306
When ever I need to kill a background process I do ps -e | grep &lt;process_name&gt;  Which prints something like this 1766 ? 00:00:13 conky , Then I use the process ID to kill it like so kill 1766 .   Is there any way I can simplify this ? Make it quicker ? reduce the amount of typing ?
116852
I use Bash as my interactive shell and I was wondering if there was an easy way to get Bash to run a system command instead of a shell builtin command in the case where they both share the same name.  For example, use the system kill (from util-linux) to print the process id (pid) of the named process(es) instead of sending a signal:  $ /bin/kill -p httpd 2617 ...   Without specifying the full path of the system command, the Bash builtin is used instead of the system command. The kill builtin doesn’t have the -p option so the command fails:  $ kill -p httpd bash: kill: p: invalid signal specification   I tried the answers listed in  but most of them only work because time is actually a shell keyword – not a shell builtin.  Other than temporarily disabling the Bash builtin with enable -n kill, the best solution I’ve seen so far is to use:  $(which kill) -p httpd   Are there other easier (involve less typing) ways to execute an external command instead of a shell builtin?  Note that kill is just an example and I’d like a generalised solution similar to the way that prefixing with the command builtin prevents functions which have the same name as an external command from being run. In most cases, I usually prefer to use the builtin version as it saves forking a new process and some times the builtin has features that the external command doesn’t.
33396
Here's my source:  #!/bin/bash  echo "Running script to free general cached memory!" echo ""; echo "Script must be run as root!"; echo ""; echo "Clearing swap!"; swapoff -a &amp;&amp; swapon -a; echo ""; echo "Clear inodes and page file!"; echo 1 &gt; /proc/sys/vm/drop_caches; echo "";   It clears caches and stuff, and it echoes that it needs to be run as root in the terminal. I basically just want the script to cease running if it detects it's not being executed as root.   Example:  "Running script to free general cached memory!" "Warning: script must be run as root or with elevated privileges!" "Error: script not running as root or with sudo! Exiting..."   If run with elevated privileges, it just runs as normal. Any ideas? Thanks!
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...?
55825
How to execute a script periodically like crontab does but without using crontab?  Please note this that the executed script should not fire with another script and schedule it with 'sleep' command, what I expect to have same functionality as crontab but it should not be present in crontab list and should not be stop when terminal or session ended,   crontab exactly but without croning it.
104056
I'm trying some things with my shell. This includes the following command:  kill $$  My expectation was that the shell would log me out and terminate itself. But instead the bash persists without change.   Any idea why?  I ran this command on different environments (archlinux, debian). The behavior was always the same.
27430
I have some conditions for some background job to run:  condition-command &amp;&amp; condition-command &amp;&amp; background-job &amp;   The problem is: I want the conditions to block until the job runs, as if I had run:  condition-command; condition-command; background-job &amp;   But it isn't a condition, if previous command fails I do not want the job to run.  I realised it is asynchronous, but it should not, in my mind the both following scripts should be the same, but they do not:  sleep 2; echo foo &amp; sleep 1; echo bar; wait   # prints foo, then bar: correct sleep 2 &amp;&amp; echo foo &amp; sleep 1; echo bar; wait # prints bar, then foo: bug   I know if I test $? variable it would work, or if I put the last one inside a subshell (but then I would lost job controls, I want to avoid daemons), but I want to know why bash make it this way, where is it documented? Is there any way to prevent this behaviour?  Edit: Chained ifs is disgusting, that is why I will not accept it as a alternative way. Edit 2: I know a subshell is possible, but it will not work for me, let us imagine I want to run a bunch of commands then wait in the end. It will be possible if i check the existence of /proc/$PID directory, but it would be a pain in the neck if there are several jobs. Edit 3: The main question is WHY bash does it, where is it documented? Whether or not it has a solution is a bonus!
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?
37624
I have a script, that does not exit when I want it to.  An example script with the same error is:  #!/bin/bash  function bla() {     return 1 }  bla || ( echo '1' ; exit 1 )  echo '2'   I would assume to see the output:  :~$ ./test.sh 1 :~$   But I actually see:  :~$ ./test.sh 1 2 :~$   Does the () command chaining somehow create a scope? What is exit exiting out of, if not the script?
38906
I want to start process (eg. myCommand) and get its pid (to allow to kill it later).   I tried ps and filter by name, but I can not distinguish process by names  myCommand ps ux | awk '/&lt;myCommand&gt;/ {print $2}'    Because processes names are not unique.  I can run process by:  myCommand &amp;   I found that I can get this PID by:   echo $!   Is there any simpler solution?  I would be happy to execute myCommand and get its PID as a result of one line command.
72672
I get how a normal fork bomb works, but I don't really understand why the &amp; at the end of the common bash fork bomb is required and why these scripts behave differently:  :(){ (:) | (:) }; :   and   :(){ : | :&amp; }; :   The former causes a cpu usage spike before throwing me back to the login screen. The latter instead just causes my system to freeze up, forcing me to hard reboot. Why is that? Both continually create new processes, so why does the system behave differently?   Both of the scripts also behave differently from  :(){ : | : }; :   which doesn't cause any problems at all, even though I would have expected them to be alike. The bash manual page states that the commands in a pipeline are already executed in a subshell, so I'm led to believe that : | : should already suffice. I belive &amp; should just run the pipeline in a new subshell, but why does that change so much?  Edit: Using htop and limiting the amount of processes, I was able to see that the first variant creates an actual tree of processes, the second variant creates all the processes on the same level and the last variant doesn't seem to create any processes at all. This confuses me even more, but maybe it helps somehow?
43519
There are several ways to execute a script. The ones I know are: /path/to/script # using the path (absolute or relative) . script        # using the . (dot) source script   # using the `source` command  Are there any other way? What are the differences between them? Are there situations that I must use one and not another?
36970
I have some applications that need to run on my laptop all the time.   I use a bash script to start these applications. In my script I have a loop that looks similar to this:    while true; do     xterm done   This runs an application (xterm in this case) and if the application crashes, the loop starts it again.    This has a disadvantage that there is no "clean" exit from this loop. So even if the intention of the user is to close xterm, the loop starts it again.    Is there a way to start an application from bash script, watch whether it is running, to re-run it if script crashed or do nothing if the user closed it properly ?
84393
If, in bash, I execute:  cmd1 | cmd2 | ... | cmdi | ... | cmdn &amp;   where the cmd{1..n} may not be distinct, how do I get the PID of cmdi? Alternatively, how can I signal the cmdi process? (For example, send it SIGUSR1?) pkill/pgrep, pidof etc. don't look like good answers, since other instances of cmdi maybe running, including as part of the same pipeline. jobs -p gives the PID of cmd1, for me.   i can be anything in {1..n}.
4366
I imagine that kill -9 still just sends a signal to a process. If the process is not behaving well, I would imagine that kill -9 would have no effect. Indeed, today for the first time I saw kill -9 have no effect when sent to a stuck ruby process. So here is my question: Is there some harsher way to kill a process?
48113
Consider this snippet:  stop () {     echo "${1}" 1&gt;&amp;2     exit 1 }  func () {     if false; then         echo "foo"     else         stop "something went wrong"     fi }   Normally when func is called it will cause the script to terminate, which is the intended behaviour. However, if it's executed in a sub-shell, such as in  result=`func`   it will not exit the script. This means the calling code has to check the exit status of the function every time. Is there a way to avoid this? Is this what set -e is for?