Find the answer to your Linux question:
Results 1 to 6 of 6
I would like to write a script that would start three programs. - The programs need to open in three different windows because they must run at the same time. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    3

    script to start three programs in different terminal windows


    I would like to write a script that would start three programs.

    - The programs need to open in three different windows because they must run at the same time.
    - They also need to start in the correct order because they need each other.
    - A time delay is needed between each program start-up.

    Here is what I wanted to use :

    #!/bin/sh
    jackd -R -d alsa -r 44100
    sleep 3s
    mplayer "$1" -ao jack -srate 44100
    sleep 3s
    brutefir /e/setup


    $1 is the filename that I want to play in mplayer and setup is a setup file for brutefir.

    That script code cannot work because I need the three programs to start in different terminal windows. This code would try to open them all in the same window. What I'm asking for is a command that would open them in different windows. I've looked at some scripting guides and it was not mentionned if that can be done and how.

    I'm sorry if that was already asked but I did not find that information by using the search.

    Also, I would like to know what are the commands to do a search in the man pages (man bash or others).


    Tril

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    maybe you can try running them all as background processes by appending an & to the end of each command? or if you really want three windows open:
    Code:
    xterm command
    that should work
    -lakerdonald

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Would something like this do the trick for you (you need the -e flag to xterm)?
    Code:
    #!/bin/sh
    xterm -e jackd -R -d alsa -r 44100 &
    sleep 3
    xterm -e mplayer "$1" -ao jack -srate 44100 &
    sleep 3
    xterm -e brutefir /e/setup &
    wait
    As for searching in manpages, that depends on whether you want to search while viewing it, or in the collection of manpages from the command line.
    The manpages are viewed with less by default (or whatever is specified in the PAGER variable). To search in less, type a / (slash), followed the regexp you wish to search for. To toggle case sensitivity, type `-i'.
    To search in the collection of manpages, you can use apropos. apropos only searches in the whatis database, though. To search the entire pages, you should just enter some compound on the command line, kind of like this to search for a regexp and report what pages it is in:
    Code:
    for f in /usr/share/man/man*/*.gz; do if zcat "$f" | grep -qi regexp; then echo "${f##*/}"; fi; done

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    3
    Thank you for your fast and accurate answer. I did not know about running background process. That worked and that was what I was looking for. I did not really need three windows.

    I used :

    #!/bin/sh
    jackd -R -v -d alsa -r 44100 &
    sleep 3s
    mplayer "$1" -ao jack -srate 44100 &
    sleep 3s
    brutefir /e/setup &

    I would like to add a command to the script that would terminate brutefir and jackd if mplayer was closed. How should I proceed.

    I also tried

    #!/bin/sh
    konsole -e jackd -R -d alsa -r 44100 &
    sleep 3
    konsole -e mplayer "$1" -ao jack -srate 44100 &
    sleep 3
    konsole -e brutefir /e/setup &
    wait

    I used konsole instead of xterm because it comes by default with kde which I am using. It opens three windows. It gives me a way of manually closing the three programs. I only need to close the three windows. I'll use that for now until I find a way to automate the closing process.

    what is wait used for? From man bash: Wait for the specified process and return its termination status. What difference does it make to use it or not.

  6. #5
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    wait just waits for those three background processes to terminate. You could skip using it, but it's more in line with standard UNIX semantics to use it in that case. If you were to take out wait, the script would launch those three processes and then terminate. They would keep running, but the shell that the script was started from would return to the command line. In other words, any process launching that script wouldn't be given the chance to wait for it. In addition, the shell and mplayer would compete for terminal input, which is often a PITA.

    If you want to terminate the other two processes when mplayer terminates, I'd do that like this:
    Code:
    #!/bin/sh
    jackd -R -v -d alsa -r 44100 &
    jpid=$!
    sleep 3
    mplayer "$1" -ao jack -srate 44100 &
    mpid=$!
    sleep 3
    brutefir /e/setup &
    bpid=$!
    wait $mpid
    kill $jpid $bpid
    $! is a special variable that contains the PID of the most recently launched background process.

  7. #6
    Just Joined!
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    3
    Thank you Dolda2000!
    I only installed linux 2 weeks ago and I still consider myself a newbie. You know more commands than I do. Your help was very helpful.

    That was exactly what I was looking for. I knew I had to kill the process but I did not know how to get their id and I also did not know that I needed to put mplayer id as an argument for wait.

    That code was all I needed. Scripts are very useful. I'll keep coming back to this forum if I ever need help again with linux.


    Tril

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •