LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-16-2014, 08:03 AM   #1
oraenthu@live.com
LQ Newbie
 
Registered: Sep 2012
Posts: 26

Rep: Reputation: Disabled
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
 
Old 03-16-2014, 07:21 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.

Last edited by jpollard; 03-16-2014 at 07:23 PM.
 
Old 03-16-2014, 09:55 PM   #3
oraenthu@live.com
LQ Newbie
 
Registered: Sep 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
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

Last edited by oraenthu@live.com; 03-16-2014 at 09:59 PM.
 
Old 03-16-2014, 10:58 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.

Last edited by jpollard; 03-16-2014 at 11:01 PM. Reason: removed accidental double post
 
1 members found this post helpful.
Old 03-17-2014, 12:04 AM   #5
oraenthu@live.com
LQ Newbie
 
Registered: Sep 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-17-2014, 08:11 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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"...
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Bash script] Executing commands in the current shell, while not launching subshells Longeron Linux - Newbie 10 01-27-2013 08:55 PM
[SOLVED] Displaying Subprocesses and Subshells with Top Mario Blunk Linux - General 2 08-26-2012 01:24 PM
Bash redirection and subshells nx5000 Programming 2 11-03-2005 07:13 AM
bash subshells have trouble with && or || on next line dfloer Linux - General 6 12-28-2004 04:50 PM


All times are GMT -5. The time now is 09:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration