Run one script, but execute commands at the same time
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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:
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?
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:
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.
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:
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:
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?)
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.
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:
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.