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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With