Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ctrl + C to terminate "grunt watch", but kills Atom editor which started from the same bash, why?

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!

like image 286
theme Avatar asked May 31 '15 09:05

theme


2 Answers

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.

like image 174
M S Parmar Avatar answered Nov 09 '22 22:11

M S Parmar


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.

like image 24
Charles Duffy Avatar answered Nov 09 '22 22:11

Charles Duffy