6

I have this script named wsjs.sh:

#!/bin/bash
WS=/home/user/wsjs
cd $WS
nohup atom . & 
gnome-terminal
grunt watch

If I run it in bash:

./wsjs.sh

Then atom editor, gnome-terminal are started separately, and the current bash showing:

user@ubuntu:~$ ./wsjs.pwd 
nohup: appending output to ‘nohup.out’
Running "watch" task
Waiting...

Now if I press ctrl + c, grunt watch quits, BUT atom editor is also closed.

...this is weird.

I manually entered every command in bash, and atom was NOT closed. I replaced atom with gedit and run the script, it was NOT closed.

Why was atom closed ? Thanks!

Jahid
  • 21,542
  • 10
  • 90
  • 108
theme
  • 238
  • 5
  • 13
  • 1
    You can [trap](http://linuxcommand.org/wss0160.php) the ctrl+c and handle it yourself to only cancel grunt watch command. – Dan Jun 02 '15 at 14:50
  • Your ctrl+c isn't killing an individual program run from the script -- it's killing the _whole script_. If you want running `./wsjs.pwd` to behave the same as running the commands in it individually would, run `source wsjs.pwd` instead of `./wsjs.pwd`. – Charles Duffy Jun 02 '15 at 14:53

2 Answers2

3

This causes because while executing shell script, it has a process id and command inside file executing will have a parent process id of script file. So while terminating or Ctl+C script file, it also terminate child processes as well (In your case

cd $WS
nohup atom . & 
gnome-terminal
grunt watch

) While executing individual command have independent process ids.

Hope you got the idea.

M S Parmar
  • 955
  • 8
  • 22
  • `nohup` without explicit redirection is considered a bad practice, since it creates `nohup.out` files. Please consider doing something explicit with stdout and stderr from `atom`, or using `disown` instead of `nohup`. – Charles Duffy Jun 02 '15 at 14:52
  • After reading this, I started to doubt it is gedit that has a strange behavior, then I find this: " [subprocesses-of-bash-gnome-terminal-dont-terminate-centos-rhel](http://unix.stackexchange.com/questions/94277/subprocesses-of-bash-gnome-terminal-dont-terminate-centos-rhel) ". I guess now I have the full story behind the scene, thanks a lot. – theme Jun 03 '15 at 07:07
  • @theme: This answer is wrong. Child processes are not normally terminated when the parent terminates. (See for example http://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits) You can see the normal behaviour with `gedit`, `xclock` etc. --- But there is a special case: the child processes receive `SIGHUP` when their session leader is terminated. Example: in Ubuntu open terminal (Ctrl+Alt+t), run `xclock &` then close the terminal using At+F4, `xclock` terminates too because of `SIGHUP`. ------ The Atom editor does behaves weirdly and I do not know why. – pabouk - Ukraine stay strong Jun 30 '15 at 02:03
2

I gather that you expect running your script to behave exactly the same way as running the commands inside it at an interactive shell.

If that's indeed your intent, then don't run

./wsjs.pwd

...which runs the script in its own shell; instead, run

source wsjs.pwd

...or its POSIX-compliant equivalent,

. wsjs.pwd ## the space is not a typo!

...which runs the script in your preexisting shell.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441