Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page How to write a simple script to start 2 programs at once?
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 07-09-2008   #1 (permalink)
rick_ca
Just Joined!
 
Join Date: Jul 2008
Posts: 17
How to write a simple script to start 2 programs at once?

I don't know much about scripting on my own, I would like to make a script that can be run from the terminal that would start 2 programs at once for example "firefox" and "gimp".

The reason I need this is to run them both over an NX remote access session so that I don't have to use a full virtual desktop (which is slower).

Thanks for any help

NEVERMIND: I found this thread: two commands on 1 line - The UNIX and Linux Forums Sorry for not searching thoroughly enough before asking
rick_ca is offline   Reply With Quote
Old 07-09-2008   #2 (permalink)
Cabhan
Linux Guru
 
Cabhan's Avatar
 
Join Date: Jan 2005
Location: Seattle, WA, USA
Posts: 2,409
So for the record, I don't believe that that thread answers your question. It is true that those answers allow you to start two programs on one line, but it is no different from starting two programs on two lines. Which is to say, it will start the GIMP, wait for you to close it, then start Firefox.

What I believe you want is to start the GIMP, then immediately start Firefox without waiting for the GIMP to be done. This is fairly simple:
Code:
#!/bin/bash

gimp &
firefox
Adding an '&' to the end of a command means "Run this command in the background". That means that you won't wait for it to finish before executing the next command.

I hope that this helps!
__________________
DISTRO=Gentoo
Registered Linux User #388732
Gentoo Linux, 410 GB HD, 1.2 GB RAM, Fluxbox, These are a Few of my Favorite Things
Cabhan is offline   Reply With Quote
Old 07-09-2008   #3 (permalink)
rick_ca
Just Joined!
 
Join Date: Jul 2008
Posts: 17
Thanks for the response, you are right that the first code doesn't work, the second example they gave works:

gimp | firefox
rick_ca is offline   Reply With Quote
Old 07-09-2008   #4 (permalink)
Lakshmipathi
Linux Enthusiast
 
Lakshmipathi's Avatar
 
Join Date: Sep 2006
Location: 3rd rock from sun
Posts: 532
Smile

Quote:
Originally Posted by rick_ca View Post
Thanks for the response, you are right that the first code doesn't work, the second example they gave works:

gimp | firefox
Just to make things bit more clear,

Code 1:
Code:
gimp | firefox
Code 2 :
Code:
gimp &
firefox
Code 2 works as Cabhan said runs a GIMP background process and you are started firefox without waiting for completion of GIMP command.

Code 1 and Code 2 are completely different.
In code1 ,

Output of GIMP is passed as an input of firefox.
So firefox will have to wait for an input which will be supplied by GIMP.



My intension is just to make sure ,
using & and | are completly two different things.



Hope this helps
Ps:I don't how to related GIMP and firefox- just take it as two separate process.
If i'm wrong please correct me.
__________________
- LakshmiPathi.G
------------------------------
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
------------------------------
My OpenSource Projects:
giis
exthide
ext3carve
Lakshmipathi is offline   Reply With Quote
Old 07-09-2008   #5 (permalink)
rick_ca
Just Joined!
 
Join Date: Jul 2008
Posts: 17
In Ubuntu the command: firefox | gimp starts both programs at the same time, so I'm not sure
rick_ca is offline   Reply With Quote
Old 07-10-2008   #6 (permalink)
Cabhan
Linux Guru
 
Cabhan's Avatar
 
Join Date: Jan 2005
Location: Seattle, WA, USA
Posts: 2,409
It is correct that both are started simultaneously with the "|", but this is a different thing.

The '&' syntax that I gave is designed exactly for the purpose: to start multiple programs without one waiting for others.

The '|' syntax is called a pipe, and it turns output of one program into the input of another. In this case, it does start both simultaneously, but you will run into big problems with some programs. Piping is very powerful and lets you connect programs in new and exciting ways, but using it carelessly will mess with a lot of things. The starting simultaneously is actually a side-effect, not the actual purpose, and piping can be implemented without starting both simultaneously.

Because the '&' syntax was designed for this reason, I suggest you use it. It also makes your purpose a lot clearer if anyone else ever looks at your script.
__________________
DISTRO=Gentoo
Registered Linux User #388732
Gentoo Linux, 410 GB HD, 1.2 GB RAM, Fluxbox, These are a Few of my Favorite Things
Cabhan is offline   Reply With Quote
Old 07-10-2008   #7 (permalink)
rick_ca
Just Joined!
 
Join Date: Jul 2008
Posts: 17
Thanks that explains some programs mysteriously crashing my server, will use the & from now on
rick_ca is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:40 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0