LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   subshells in linux (https://www.linuxquestions.org/questions/linux-newbie-8/subshells-in-linux-4175498377/)

oraenthu@live.com 03-16-2014 07:03 AM

subshells in linux
 
Hi all,

2.6.18-128.4.1.0.1.el5 #1 SMP Tue Aug 4 15:23:10 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

(run by command w disowning);(run by command x disowning);
(run by command y disowning);(run by command z disowning);

this I believe should run w then x then y then z each in a separately spawned shell
is it supposed to show x,y,z when executing w
I see them (x,y,z) when w is executing.

Thanks

jpollard 03-16-2014 06:21 PM

Depends on what you mean by "show x,y,z when executing w".

If you don't redirect input/output from/to appropriate files, then all of that input/output will be ATTEMPTED from the same command window. Which means you don't know which application may have requested input (and hanging all the others), or which output belongs to which command...

Now sometimes the output doesn't matter - the case of status messages from various commands (such as scp, tar ...), but can get confusing if running the same command with different contexts (like creating multiple tar files from different directories in parallel).

In more common terms, these are just other processes, organized such that the command shell that started them is known to be the parent process.

Usually the term "subshell" is used within a shell script where things like loops, functions, and such may get run as separate processes, but don't have to be.

oraenthu@live.com 03-16-2014 08:55 PM

The commands here are files/scripts prefixed with nohup and suffixed with an ampersand
(nohup sh scriptname_w.sh &);(nohup sh scriptname_x.sh &);(nohup sh scriptname_y.sh &);(nohup sh scriptname_z.sh &);
specifically in the command above will they be sequentially executed?
I was not able to run them without the parenthesis, what I received was a "syntax error near ';'"
This worked when I created two scripts abc.sh and def.sh,
abc.sh created a file using touch command
touch abc.txt
and def.sh moves that file to a directory above the current directory
mv abc.txt ../
This worked fine-sequentially.So I used the same to run my prod scripts.
Question: Does this work sequentially?
Am I required to write the ampersand outside the bracket as well/both inside and outside/only outside.
What options do I have with position of ampersand?
and lastly when we do a
cd somedir;ls -al
it works just fine, what makes the nohup sh scriptname.sh ampersand to not follow the usual thing and show a syntax error
Thanks you

jpollard 03-16-2014 09:58 PM

No, as soon as the first command is put into the background, the shell proceeds to interpret and execute the second - which is also put into the background.

The & symbol in this context puts the preceding command into the background. It also functions as a command terminator.

If you wanted these to run serially that is also possible - but you do it by putting the entire command line inside parenthises (to group the string), with each command separated by a ";" and then put a single & at the end:as in (cmd1; cmd2; cmd3) &

Alternatively you can use: bash -c 'cmd1; cmd2; cmd3' &
which accomplishes the same thing. Redirection is also possible: bash -c 'cmd1; cmd2; cmd3' >log 2>&1 &

The "2>&1" construct causes stderr to be redirected to the same file as stdout. This redirection works with the () construct as well.

The "nohup" command separates the process that executes the command(s) from the controlling terminal. This is required for some shells, and is a good idea for others - it allows you to log out and the commands will continue to run. This isn't always required (it is up to the specific shell as to whether background processes are detached or not - and it also depends on whether all the stdin/stdout/stderr has been redirected from the terminal.

oraenthu@live.com 03-16-2014 11:04 PM

It looks like this(whether execution of the commands is serial) is contingent to the task.
Please give me an example other than the one I am facing where it is not serial.
Sorry to bother you.
Feel absolutely free to not provide an example.
(nohup sh script_w.sh;nohup sh script_x.sh;nohup sh script_y.sh) &
This is what you suggested right?
Thanks for your time.

jpollard 03-17-2014 07:11 AM

In your case you don't need the parenthesis. Doesn't hurt, but also doesn't really help either.

Your command is also missing quotes to make "script_w.sh;nohup sh script_x.sh;nohup sh script_y.sh" a parameter to sh.

This should have been written as
Code:

nohup sh -c "script_w.sh; script_x.sh; script_y.sh" &
or
Code:

nohup sh -c 'script_w.sh;nohup sh script_x.sh; script_y.sh' &
The use of the " quoting allows shell variables to be substituted into the string.

It serves no real purpose to using "nohup" within a "nohup"...


All times are GMT -5. The time now is 07:40 AM.