Find the answer to your Linux question:
Results 1 to 8 of 8
Hi Im programing a perl script which executes commands on a terminal i.e.: `mkdir /some_directory/`; Heres my question, is there some way to know that the command actually finished its ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    6

    linux terminal question


    Hi

    Im programing a perl script which executes commands on a terminal i.e.: `mkdir /some_directory/`;

    Heres my question, is there some way to know that the command actually finished its execution... For example if I do something that is much more time consuming for example getting some URL, is there someway of checking within the script that the command has finished?

    thnx

  2. #2
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    You could fork the command as a child process and check for it to return.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  3. #3
    Just Joined!
    Join Date
    Aug 2005
    Posts
    6
    what do you mean by forking???

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer
    Join Date
    Apr 2005
    Location
    Belgium
    Posts
    1,429
    I know in bash if you don't put & at the end of the command, it will not continue to the next command until the one it's executing is finished.
    For example

    Code:
    updatedb; echo "Finished updating search index"
    will display a 'silence' at the CLI for a long time, and only echo the string above when updatedb is finished.

    However
    Code:
    updatedb &; echo "Finished updating search index"
    will just execute updatedb, and won't wait for it to finish; immediately after starting updatedb it will echo the string.

    Also, in both cases, if updatedb returns an error, it will display on screen (unless you redirect the error output to /dev/null).
    ** Registered Linux User # 393717 and proud of it ** Check out www.zenwalk.org
    ** Zenwalk 2.8 - Xfce 4.4 beta 2- 2.6.17.6 kernel = Slack on steroids! **

  6. #5
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Quote Originally Posted by pelonbiologo
    what do you mean by forking???
    I was thinking of the way you run processes in C. I actually haven't forked in a shell script in a long time, so I don't remember how it works... I'd go with borromini's suggestion.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  7. #6
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86
    Try exporting the parent's process id to the child (I don't think that there is an existing bash environement variable) and have the child signal the parent on completion. The parent traps the signal from the child and then acts accordingly. For example:

    script "alpha":
    Code:
    #!/bin/bash
    function beta_complete {
     echo "beta complete"
     exit 0
    }
    trap beta_complete SIGINT
    export parent=$$
    
    echo "beta start"
    ./beta
    sleep 10000
    exit0
    script "beta"
    Code:
    #!/bin/bash
    sleep 1
    kill -s SIGINT $parent
    exit 0
    You can explicitly spawn child threads from within a bash script, however this method of creating childern is quick and easy.

  8. #7
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86
    oops, my bad, bash isn't perl, however I'm sure you could use a similar process to suit your needs.

  9. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Taken from "Learning Perl" 3rd Edition from O'Reilly:

    Code:
    defined(my $pid = fork) or die "Cannot fork: $!";
    unless ($pid) {
        # Child process is here
        exec "date";
        die "cannot exec date: $!";
    }
    # Parent process is here
    waitpid($pid, 0);

    Here is what this is doing:

    You are first using the "fork" command to generate a child process. You then get to the unless statement, and because only the parent process has a nonzero PID, the parent skips the statement, while the child goes into the statement.

    The parent then waits for the child to finish before continuing.


    Hope this helps!

Posting Permissions

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