Running in background


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running in background
# 1  
Old 07-21-2011
Running in background

Hello, I was trying to make some processes to run at background and went to a problem.

First I tried just to loop in one line something like this:

Code:
for i in {1..10}; do echo 'hello world' &; done;

but it pops a syntax error, so I tried several ways to fix it but wasn't able to understand the use of & operator.

So I came to this:

Code:
f(){ echo 'Hello World' & }; for i in {1..10}; do f; done;

And it worked around the problem.

Still, I really want to know how the & operator works exactly so I wonder if someone is able to explain it or point me somewhere to go!

Thanks!

Last edited by radoulov; 07-21-2011 at 07:23 PM.. Reason: Code tags.
# 2  
Old 07-21-2011
Which shell are you using?

In bash, for example, & is a control character that terminates a simple (in this case) command by itself and the following one is not permitted:


Quote:
control operator
A token that performs a control function. It is one of the following symbols:
|| & && ; ;; ( ) | |& <newline>
[...]

SHELL GRAMMAR
Simple Commands
A simple command is a sequence of optional variable assignments followed by blank-separated words and redirec-
tions, and terminated by a control operator.
Try omitting the semicolon:


Code:
4.1.10(4)-release$ for i in 1 2; do echo ok& done
[1] 6340
[2] 4292
ok
4.1.10(4)-release$ ok

[1]-  Done                    echo ok
[2]+  Done                    echo ok

Some shells accept that syntax though:

Code:
4.1.10(4)-release$ zsh -c 'for i in 1 2; do echo ok&; done'
ok
ok

# 3  
Old 07-21-2011
Yes I'm using bash.

That was it! Thanks!

Since & terminates the command, bash - at least this version I'm using (4.1.5) - would not accept &;.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running script in background

Hi, I wrote a KSH script and running it on HP-UX machine I am running one script in background. My script is at location $HOME/myScript/test/background_sh When I view my script in background with psu commend > psu | grep background_sh I see following output UID PID PPID C ... (1 Reply)
Discussion started by: vaibhav
1 Replies

2. Shell Programming and Scripting

Running scripts in background

Hi, below is my master script wihch inturn runs 2 scripts in background #master_script.sh ./subscript1.sh & ./subscript2.sh & executed the master_script.sh from unix command prompt $ ./master_script.sh it is executing the subscripts and they are completing fine, however master_script.sh is... (2 Replies)
Discussion started by: JSKOBS
2 Replies

3. Shell Programming and Scripting

Running two commands in background

Hi I want to run two commands in background, i am using below way, but it is not working, i m calling these two commands from one script to another server where below commands are running, in my script ssh is working fine, but command is not going in background, please advise what should i do. ... (10 Replies)
Discussion started by: learnbash
10 Replies

4. Solaris

running command in background

hi all i am running following command ufsdump 0ubf 512 /dev/rmt/0cbn /database/backup2/rman_backup/level1 >> /database/backup2/backup_tape/level1_rman_03aug12 2>&1; i want to run it in background how can i do it in this i am generating logs for backup. the problem occurs when i am... (3 Replies)
Discussion started by: nikhil kasar
3 Replies

5. Solaris

About running processes in background

Hi, I need to establish a procedure that will start an application in background each time my remote Solaris server is (re)started. This would be a kind of daemon. I am no sysadmin expert, so I am looking for pointers. How should I proceed? What are the main steps? Thanks, JVerstry (9 Replies)
Discussion started by: JVerstry
9 Replies

6. UNIX for Dummies Questions & Answers

Running the Script in Background.

Gurus, Pls. help on this to run the script in background. I have a script to run the informatica workflows using PMCMD in script. Say the script name is test.sh & Parameters to the script is Y Y Y Y The no of parameters to the bove script is 4. all are going to be a flags. Each flag will... (2 Replies)
Discussion started by: prabhutkl
2 Replies

7. Shell Programming and Scripting

Running RSH on the background

Hi, I am developing a script that would do a 'rsh' on a client machine. I want to invoke a script in client machine which would keep on polling data to the server. I want the rsh to return back once it invoked the script on client and the script should run on background on client. ... (2 Replies)
Discussion started by: eamani_sun
2 Replies

8. Shell Programming and Scripting

running in background

i have a script called server.sh. how to run this script in backgroung using nohup command (5 Replies)
Discussion started by: ali560045
5 Replies

9. UNIX for Dummies Questions & Answers

running process in background

I'm trying to install a solaris 9 patch cluster and when I try to use & to run in background it won't allow me to enter in my sudo password so it fails the install and sudo auth. Does Solaris not have screen like linux? If & will work what am I doing wrong? sudo ./install_cluster -q & is... (3 Replies)
Discussion started by: kingdbag
3 Replies

10. Shell Programming and Scripting

Running two processes in background

hi there, here's what i need in my korn-shell: ... begin korn-shell script ... nohup process_A.ksh ; nohup process_B.ksh & ... "other stuff" ... end lorn-shell script in plain english i want process A and process B to run in the background so that the script can continue doing... (6 Replies)
Discussion started by: jacob_gs
6 Replies
Login or Register to Ask a Question