Sponsored Content
Top Forums Shell Programming and Scripting How to put FTP process as a background process/job in perl? Post 302539527 by pludi on Monday 18th of July 2011 04:31:49 AM
Old 07-18-2011
Do you want to do any additional processing while the file is being transferred? If not, I don't see why the transfer process should be separated from the main process.

Otherwise, here's a small script on how to use fork:
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

print "Parent script\n";

my $pid = fork();
if ( $pid == 0 ) {
    print "Child script started\n";
    sleep 5;
    print "Child script ending\n";
    exit int( rand(255) );
}
elsif ( $pid > 0 ) {
    print "Parent script, child has PID $pid\n";
    waitpid( $pid, 0 );
    print "Parent script, child exited with status " . ( $? / 256 ) . "\n";
    exit 0;
}
else {
    print "Error when forking: $!\n";
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies

2. Shell Programming and Scripting

killing unix job after the job process completes

Hi, Thanks in advance. i need to kill a unix background running job after that job process completes. i can kill a job by giving the following unix command kill -9 processid how to kill the job after the current process run gets completed ? Appreciate your valuable help. ... (1 Reply)
Discussion started by: dtazv
1 Replies

3. Solaris

killing a unix job after the job process gets completed

Hi, Thanks in advance. i need to kill a unix background running job after that job process completes. i can kill a job by giving the following unix command kill -9 processid how to kill the job after the current process run gets completed ? Appreciate your valuable help. Thanks... (7 Replies)
Discussion started by: dtazv
7 Replies

4. Shell Programming and Scripting

Perl Background Process - Finshed Yet?

I have a perl process I want to run in background in a cgi, but do not want to continue until process finished code looks like this. sub calc(){ do calculations return value } In another Perl program I call above function such as &calc(); I want to continue after process... (4 Replies)
Discussion started by: photon
4 Replies

5. Solaris

background process

Hi When I run ./script.sh & the script runs in bg But when I close the telnet session, the script is killed also. any idea how to keep this script running? thx (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

6. Shell Programming and Scripting

How to process and run a program in the background in perl?

Hi, I have a query about processing and running Perl program at the background. I have HTML file called Userform.html which accepts input from the user. As soon as input is given the contol goes to get.cgi (get.cgi does some processing and computing tasks). Actually get .cgi takes more... (0 Replies)
Discussion started by: vanitham
0 Replies

7. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

8. Shell Programming and Scripting

How can put a background process to the foreground

Hi, guys: I am working on my own shell using c. When I put a process into the background, how can I put it back to the foreground using tcsetpgrp? Thanks (3 Replies)
Discussion started by: tomlee
3 Replies

9. Shell Programming and Scripting

How to call a background process in perl?

Hi, I want to put the following code as a parallel or background process The program is as below: $n=10; #Count of files to be created. for($j=0;$j<=$n;$j++) { open(FH,">files_$j.txt") || warn "cannot create a file\n"; { print FH "count of file: $j\n"; #Sample data to be written. just... (5 Replies)
Discussion started by: vanitham
5 Replies

10. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies
PCNTL_WAITPID(3)							 1							  PCNTL_WAITPID(3)

pcntl_waitpid - Waits on or returns the status of a forked child

SYNOPSIS
int pcntl_waitpid (int $pid, int &$status, [int $options]) DESCRIPTION
Suspends execution of the current process until a child as specified by the $pid argument has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function. If a child as requested by $pid has already exited by the time of the call (a so-called "zombie" process), the function returns immedi- ately. Any system resources used by the child are freed. Please see your system's waitpid(2) man page for specific details as to how wait- pid works on your system. PARAMETERS
o $pid - The value of $pid can be one of the following: possible values for $pid +-----+---------------------------------------------------+ | | | |< -1 | | | | | | | wait for any child process whose process group | | | ID is equal to the absolute value of $pid. | | | | | | | | -1 | | | | | | | wait for any child process; this is the same be- | | | haviour that the wait function exhibits. | | | | | | | | 0 | | | | | | | wait for any child process whose process group | | | ID is equal to that of the calling process. | | | | | | | |> 0 | | | | | | | wait for the child whose process ID is equal to | | | the value of $pid. | | | | +-----+---------------------------------------------------+ Note Specifying -1 as the $pid is equivalent to the functionality pcntl_wait(3) provides (minus $options). o $status -pcntl_waitpid(3) will store status information in the $status parameter which can be evaluated using the following functions: pcntl_wifexited(3), pcntl_wifstopped(3), pcntl_wifsignaled(3), pcntl_wexitstatus(3), pcntl_wtermsig(3) and pcntl_wstopsig(3). o $options - The value of $options is the value of zero or more of the following two global constants OR'ed together: possible values for $options +----------+---------------------------------------------------+ | | | | WNOHANG | | | | | | | return immediately if no child has exited. | | | | | | | |WUNTRACED | | | | | | | return for children which are stopped, and whose | | | status has not been reported. | | | | +----------+---------------------------------------------------+ RETURN VALUES
pcntl_waitpid(3) returns the process ID of the child which exited, -1 on error or zero if WNOHANG was used and no child was available SEE ALSO
pcntl_fork(3), pcntl_signal(3), pcntl_wifexited(3), pcntl_wifstopped(3), pcntl_wifsignaled(3), pcntl_wexitstatus(3), pcntl_wtermsig(3), pcntl_wstopsig(3). PHP Documentation Group PCNTL_WAITPID(3)
All times are GMT -4. The time now is 06:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy