perl@newbie has asked for the wisdom of the Perl Monks concerning the following question:

Hej Monks, I think i have caused too much confusion. I will rephrase the question.
First of all the environment: Linux(tcsh shell) and perl version 5.8.3. Problem: I want to do something like this
system("long command to be executed on the tcsh in the background &").
If i run this long command from the prompt it doesnt give me any error. But If i run it using system and exec inside perl script (with or without fork), command is executed succesfully, prompt is returned but i get an error message on the prompt. Scenarios that i have tried to perform as suggested by you all
=>using system function - <code<system("command &") && die(" From Sys func: $!"); </code> Executes my command and Returns the prompt but it gives an error after returning the prompt "write to host (-1) failed in SendMessageToHost" Note that i dont get illegal seek this time and the error mesg is not coming from die
=> using fork as suggested by cowboy with exec for executing the command on the shell exec(" command &") && die("From Exec function: $!"); Prompt returns, gets the similar error "write to host (-1) failed in SendMessageToHost"
=> using sys command inside fork or otherwise but having die as an OR condition system("command &") || die("From sys function: $!"); This time i get "From sys function: Illegal seek" error and then the prompt returns and gives the error "write to host (-1) failed in SendMessageToHost"
Most of you have pointed out that the error may be coming from the command. Command works fine for me when i run it on the prompt. And i get these errors only from within perl script. Thanx

Replies are listed 'Best First'.
Re: Run the process in the background
by polettix (Vicar) on Aug 31, 2005 at 13:31 UTC
    How do I post a question effectively? is a good start to solve your problem. In brief, please post:
    • the system you're using
    • the perl version
    • what that cmd is and does
    Chances are that your problem is with the cmd and not with perl, anyway.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Run the process in the background
by puploki (Hermit) on Aug 31, 2005 at 13:29 UTC

    If you want to start something into the background and not wait for it to finish before returning to Perl, use fork() (which actually threads under Windows)

    Alternatively you can use the Windows command start to start something in the background. You can use system( "start mycommand.exe" ); from Perl

    I hope that's the question you're asking... ;) I think the error message you're seeing is from your application rather than Perl. You can put system( "command" ) and die "$!\n"; to see if the application returns anything over STDERR as well to check.

    Update:The poster had previously not mentioned the OS being used - the use of cmd fooled me into thinking it was Windows :)

      Hej Monks, I think i have caused too much confusion. I will rephrase the question.
      First of all the environment: Linux(tcsh shell) and perl version 5.8.3. Problem: I want to do something like this
      system("long command to be executed on the tcsh in the background &").
      If i run this long command from the prompt it doesnt give me any error. But If i run it using system and exec inside perl script (with or without fork), command is executed succesfully, prompt is returned but i get an error message on the prompt. Scenarios that i have tried to perform as suggested by you all
      =>using system function - system("command &") && die(" From Sys func: $!"); Executes my command and Returns the prompt but it gives an error after returning the prompt "write to host (-1) failed in SendMessageToHost" Note that i dont get illegal seek this time and the error mesg is not coming from die
      => using fork as suggested by cowboy with exec for executing the command on the shell exec(" command &") && die("From Exec function: $!"); Prompt returns, gets the similar error "write to host (-1) failed in SendMessageToHost"
      => using sys command inside fork or otherwise but having die as an OR condition system("command &") || die("From sys function: $!"); This time i get "From sys function: Illegal seek" error and then the prompt returns and gives the error "write to host (-1) failed in SendMessageToHost"
      Most of you have pointed out that the error may be coming from the command. Command works fine for me when i run it on the prompt. And i get these errors only from within perl script. Thanx
Re: Run the process in the background
by ikegami (Patriarch) on Aug 31, 2005 at 13:43 UTC

    If you're in Windows and "cmd" expects a list of hosts as arguments, "&" would be treated as a host name since "&" doesn't mean anything to the Windows shell. Understandably, "cmd" can't connect to host "&".

    system("start cmd") is Windows's equivalent to unix's system("cmd &").

      I think when he said "cmd" he meant something like $cmd which contains the command. I don't think this question has anything to do with windows, he probably should have said, system('my_long_command_here &') or something.

        I know. I never assumed he was refering to cmd.exe. (I assumed the opposite, since cmd.exe doesn't accept host names are arguments.) I described the most likely scenario given the lack of details posted by the OP, and the solution for that problem.

Re: Run the process in the background
by polettix (Vicar) on Aug 31, 2005 at 15:40 UTC
    Im using this site for the first time. Please bear with me.
    Welcome to the Monastery. You can always find good help about how Perl Monks works using the Need Help?? link that you find top right of every page. In particular, you shouldn't modify your posts like you did, otherwise the answers you already got are put out of context and make people who answered you seem silly. How do I change/delete my post? is a good reference about post changing etiquette.

    Back to business, are you sure that the problem is in the Perl script and not in the command you invoke?

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Run the process in the background
by Codon (Friar) on Aug 31, 2005 at 16:14 UTC
    Could it be that "cmd" is actually reporting the "Illegal Seek"? Make sure that your die() is not simply die $! but has something more telling such as:
    system("cmd &") && die "cmd failed: $!\n";
    Note: system() returns 0 on success. If you have system() || die() you are trying to die on success. That, I'm assuming, would be bad.

    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.
Re: Run the process in the background
by cowboy (Friar) on Aug 31, 2005 at 18:47 UTC
    Fork should do what you want.
    my $pid = fork(); unless (defined($pid)) { die "Fork failed: $!"; } unless ($pid) { # we're the child exec("cmd"); # execute the command, terminating the child. } exit; # exit parent