LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2007, 09:14 PM   #1
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
Run one script, but execute commands at the same time


Strange situation, but how do you execute commands in a single file script to execute at the same time without waiting for the first command to finish? For example:

Code:
#!/bin/sh
cp bigfile.iso ~/
hostname >> computername.txt
So in this case, I would have to wait for the bigfile.iso to finish copying and then it will run the hostname command into a text file. however, I want them to to happen simultaneously. Any way to do this in a single shell script file?
 
Old 07-17-2007, 09:27 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Code:
#!/bin/sh
cp bigfile.iso ~/ &
hostname >> computername.txt
Not exactly 'simultaneously', but command2 will execute before the backgrounded command1 finishes.
 
Old 07-17-2007, 09:30 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
You could use the following:
Code:
#!/bin/sh
cp bigfile.iso ~/ &
hostname >> computername.txt
wait
The wait statement is to prevent the script exiting and leaving the cp command as an orphan process. There's some info on wait at http://tldp.org/LDP/abs/html/x8573.html#WAITHANG
 
Old 07-17-2007, 09:43 PM   #4
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Hmm, adding the & doesn't appear to work, but I should have been more realistic in my example. I'm actually not using cp, but actually running a program called 'R', which does some statistical analysis. Does that make a difference? Basically this 'R' program runs and takes days for it to finish doing its thing. In the meantime, I need it to output the hostname to a text file, but it's not happening. The shell script still sits there and waits for the first line, R, to finish, and then it runs the hostname command.
 
Old 07-17-2007, 10:11 PM   #5
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
It shouldn't matter what command is being pushed to the background. If you run the script with the wait command in it, the main script will execute all of its commands and then wait until the background process is finished before exiting. If you leave out the wait statement it will exit without waiting for the background process to finish.

To see what is happening, you can run it with -x to see debug output:
Code:
#!/bin/sh -x
cp bigfile.iso ~/ &
hostname >> computername.txt
wait
 
Old 07-17-2007, 10:46 PM   #6
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
I realized what was going on, and I see that the -x helped me see what was going on. Basically the R program is writing to a file, and I wanted the hostname on to begin on this file, but I think it won't work because R keeps writing to the file from scratch, thus overwriting the hostname at the beginning of the file (does that make sense?)

R inputfile outputfile &
hostname >> outputfile

And unfortunately R doesn't work like this

hostname >> outputfile
R inputfile >> outputfile

Last edited by Micro420; 07-17-2007 at 10:47 PM.
 
Old 07-17-2007, 11:07 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You probably want to use "screen" to be able to log out and at a later time or date, log in again and reattach. Another thing you can use is to use "nohup" to detach the process and have it run in the background. Using nohup, you could redirect the output to a file, or let it write to the default nohup.out file. You can't re-attach to a terminal if you use nohup.
 
Old 07-17-2007, 11:23 PM   #8
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Instead of hostname >> outputfile you could use this:
Code:
#!/bin/sh
/path/to/R
sed -i -e "1 i\
`hostname`
" computername.txt
It doesn't background the R command, it just waits until it's finished and uses sed to insert the hostname at line 1.
 
Old 07-18-2007, 12:05 AM   #9
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
I just realized that even if the R program crashes, the shell script will still execute the next command, hostname, since it is indepedent of R so I will still be able to grab the hostname information I need.

Good suggestion, gilead. It would be nice to have the hostname at the top of the output file and will use sed to stick it at the top for easy readability. Thanks all!
 
  


Reply



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
Is a script, run at boot time from init.d, run with root authority? tmbrwolf53 Linux - Server 2 03-31-2007 08:15 PM
execute php script by a time () afbase Linux - General 1 12-30-2006 03:30 PM
I need to run commands every time I want my wireless card to work... sloik2000 Linux - Wireless Networking 4 12-17-2006 11:29 AM
can ssh execute multiple commands at the same time? Singist Linux - Networking 2 04-04-2006 09:14 AM
run 2 commands in a script ddpicard Linux - General 10 06-13-2003 04:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 AM.

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