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

I have this script called wsjs.sh :

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

If I ran it in bash:

 ./wsjs.sh 

Then the gnome-terminal atom editor starts separately, and the current bash shows:

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

Now, if I press ctrl + c , the grunt watch exits, BUT the atom editor also closes.

... this is strange.

I manually entered each command in bash and the atom was not closed. I replaced the gedit atom and ran the script, it was NOT closed.

Why did the atom close? Thanks!

+6
source share
2 answers

This is due to the fact that during the execution of the shell script it has a process identifier, and the command inside the executable will have the parent process identifier of the script. Thus, upon completion of either the Ctl + C script, the file also terminates the child processes (in your case

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

) When executing a separate command, there are independent process identifiers.

Hope you have an idea.

+3
source

I understand that you expect your script to behave exactly like running commands inside it in an interactive shell.

If this is really your intention, then do not run

 ./wsjs.pwd 

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

 source wsjs.pwd 

... or its equivalent compatible with POSIX,

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

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

+2
source

Source: https://habr.com/ru/post/988258/


All Articles