Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

problems with fork

by Anonymous Monk
on May 15, 2002 at 08:32 UTC ( [id://166667]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

dear monks.

I am trying to fetch a big file from a server to another server. I log to my server using ssh.. and I trying to create a script that forks to download, so I can exit my ssh without loosing the download. the code is still waiting for the download. I am not very good with forking, what I want is to be able to run this script, have a forked process download, and safely close my ssh login.
here's what I came up with but it obviously is not working
#!/usr/bin/perl use LWP::Simple; if (fork) { getstore('ftp://someserver/big640Mbfile', '/myfolder/file'); }
should I better place a crontab to start the file?

Replies are listed 'Best First'.
Re: problems with fork
by kappa (Chaplain) on May 15, 2002 at 10:07 UTC
    You can use UN*X command nohup to start any program you want in the background and be sure that it won't die when you disconnect. Look:
    $ nohup wget ftp://ftp.server/bigfile appending output to nohup.out $
Re: problems with fork
by perigeeV (Hermit) on May 15, 2002 at 10:33 UTC

    Your getstore is being executed by the parent. fork returns the PID to the parent, so the truth result of your if conditional means you are in the parent. A return value of 0 means you're inside the child. Also check a return of undef to make sure fork didn't puke.


Re: problems with fork
by yodabjorn (Monk) on May 15, 2002 at 11:33 UTC
    nohup is good, but I would also sugest the most excellent program screen for running your cmds in a remote terminal or wanting to close a term and leave stuff running. Screen is great for coding on remote servers becasue you can detach/reattach to your remote session without loosing anything ( like if you on dialup adn you are writing code on a server and loose connection you can reconnect and re=attach to your screen session)

    example:
    ssh foo
    foo> screen
    foo> cat /dev/zero&
    bunches of zeros!
    foo> screen -d OR close xterm

    new term:
    > ssh foo
    foo> screen -d -RR bunches of zeros! from original session

    Screen is a virtual Terminal it rocks and It's one othe those programs that after you start using it you wonder how you ever lived without it! ( I sure do! ). Other great thing is mutiple virtual terminals in a single term (C-a c, C-a n)

    if you are gonna fork then I would reccomend a nice module called Parallel::ForkManager

    enjoy!
Re: problems with fork
by perlplexer (Hermit) on May 15, 2002 at 12:26 UTC
Re: problems with fork
by BazB (Priest) on May 15, 2002 at 20:49 UTC

    If you want to fork, have a look at BrentDax's ForkBlock, in particular the phork subroutine which does a fork and retries temporary errors, to see how to do it.
    IIRC, there is similar code in The Camel.

    Cheers.

    BazB.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://166667]
Approved by moodster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found