LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-12-2020, 11:26 AM   #1
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 9, SH4, Debian
Posts: 367

Rep: Reputation: 8
Run set of commands in background in bash


Lets take such bash script:

Code:
#!/bin/bash
-----
<begining of script>
-----
command1 ; command2 ; command3
-----
<rest of script>
How to run "command1 ; command2 ; command3" in background? I mean that to execute in background first command, when it ends run second and when it ends run third one.
Of course it is possible to place such set of commands in separate script and whole run in background using "&" directive.
But how to make it in original script?

Last edited by mackowiakp; 09-12-2020 at 11:27 AM.
 
Old 09-12-2020, 11:32 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
I would try

Code:
#!/bin/bash
-----
<begining of script>
-----
(command1 ; command2 ; command3) &
-----
<rest of script>
or

Code:
#!/bin/bash
-----
<begining of script>
-----
sh -c "command1 ; command2 ; command3" & 
-----
<rest of script>
Both of those will run the three commands in sequence in the background while the rest of the script runs its course.
 
1 members found this post helpful.
Old 09-13-2020, 04:18 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A (subshell) is a complete shell code.
Code:
#!/bin/bash
...
# sub shell starts here
(
  command1
  command2
  command3
) </dev/null >/dev/null 2>&1 &
# main shell continues here
...
The redirections separate stdin,stdout,stderr from a terminal i/o.
If you run the main shell in a terminal then the sub shell will continue when you exit or logout.
 
1 members found this post helpful.
  


Reply

Tags
background, bash, command



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
[SOLVED] In terminal (bash), when you've run commands, and then you press the up arrow and all the commands are there, how to > to a text file. bk328115 Linux - Newbie 4 08-03-2018 10:57 PM
[SOLVED] most shell commands I run are sent to background eco Linux - General 2 11-22-2012 12:11 PM
I want to run php file in background on fedora terminal in background. gauravwcities Linux - Newbie 8 01-18-2012 12:23 AM
LXer: How to run commands in the background LXer Syndicated Linux News 0 10-10-2010 10:30 AM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 12:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:42 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
Open Source Consulting | Domain Registration