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

process run in background

by t-rex (Scribe)
on Jul 27, 2016 at 11:48 UTC ( #1168634=perlquestion: print w/replies, xml ) Need Help??

t-rex has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am working on a module which is based on client-server socket model such that

client –-->tries to connect to particular server (socket) if ( fails) then client--→ transfers a tar file and a script to the server machin +e this script untar the folder and runs server.pl on the remote server m +achine
I have achieved this functionality but only issue is the above server runs in while(1) hence when I launch the server.pl on remote server from client , my client gets hanged so I have to give a ctrl+Z to make server.pl run in the background and start the client again. This is causing some hiccups in my module ( basically it is desired to just launch one script which in turn will do everything for you). I have tried nohup command & but of no use. I haven’t shared the code as it is huge , and I want you guys to make explain what exactly I am doing. I can share the code if need be , please help

Replies are listed 'Best First'.
Re: process run in background
by stevieb (Canon) on Jul 27, 2016 at 11:59 UTC

    I like Proc::Background for this task, as it is cross-platform (*nix and Windows).

    It's as simple as:

    my $bg = Proc::Background->new(@cmd_and_args); my $pid = $bg->pid;

    I've used it in a more complex way with start/stop/status functionality with run-in-foreground ability, where the server code is in a module: server module (the bg proc is actually created on line 106), and then a script that starts, stops etc the daemon: server control script.

    Essentially how it works is that the script is called from the CLI, which in turn executes the module start code, which re-calls the script with a different argument, which calls the listener/network code in the module which is put into the background.

Re: process run in background
by stevieb (Canon) on Jul 27, 2016 at 16:44 UTC

    I decided to write a much simpler, single-file example for you to examine using Proc::Background. I'll start with the starting, examining, input/output and stopping, then I'll show the code.

    start the daemon

    $ ./script.pl start started script.pl daemon at PID 30216

    verify it's running

    $ ./script.pl status script.pl is running at PID 30216 $ ps ax | grep 30216 30216 pts/4 S 0:00 perl script.pl perform

    connect to the daemon

    $ telnet localhost 6669 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.

    enter some input, and ensure the network daemon returns correctly

    blah received: blah

    network daemon closes connection

    Connection closed by foreign host.

    ensure the daemon is still running

    $ ./script.pl status script.pl is running at PID 30216 $ ps ax | grep 30216 30216 pts/4 S 0:00 perl script.pl perform

    stop the daemon

    $ ./script.pl stop stopped script.pl PID 30216

    ensure it's really stopped

    $ ./script.pl status script.pl is not running $ ps ax | grep 30216

    Code. Note that although the usage printout doesn't show perform, if you run the script like ./script perform, it'll run in the foreground instead of going into the background. Note also I use $0 which holds the name of the script file, so no matter what you name the file, the output will always reflect the current script.

    #!/usr/bin/perl use warnings; use strict; use IO::Socket::INET; use Proc::Background; if (! @ARGV || $ARGV[0] !~ /(?:start|stop|status|perform)/){ die "usage: $0 <start|stop|status>\n" } my $op = $ARGV[0]; my $pid_file = '/tmp/proc.pid'; # this... start() if $op eq 'start'; stop() if $op eq 'stop'; status() if $op eq 'status'; perform() if $op eq 'perform'; # could also be written in a more concise way... #{ # no strict 'refs'; # &$op(); #} sub start { if (status(1)){ print "$0 already running at " ._get_pid(). "\n"; exit; } my $proc = Proc::Background->new("perl $0 perform"); my $pid = $proc->pid; print "started $0 daemon at PID $pid\n"; open my $wfh, '>', $pid_file or die $!; print $wfh $pid; close $wfh; } sub stop { my $pid; if (status(1)){ $pid = _get_pid(); kill 'KILL', $pid; unlink $pid_file; print "stopped $0 PID $pid\n"; } else { print "$0 doesn't appear to be running\n"; } } sub status { my $quiet = shift; my $pid = _get_pid(); my $status = $pid ? 1 : 0; if ($status){ print "$0 is running at PID $pid\n" if ! $quiet; } else { print "$0 is not running\n" if ! $quiet; } return $status; } sub perform { my $sock = new IO::Socket::INET ( LocalHost => '0.0.0.0', LocalPort => 6669, Proto => 'tcp', Listen => 5, Reuse => 1, ); die "cannot create socket $!\n" unless $sock; while (1){ my $conn = $sock->accept; # below is what we receive from the client my $recv; $conn->recv($recv, 1024); # ...and this is what we send back my $send = "received: $recv\n"; $conn->send($send); shutdown($conn, 1); } $sock->close; } sub _get_pid { my $pid; if (-e $pid_file){ open my $fh, '<', $pid_file or die $!; $pid = <$fh>; close $fh; } return $pid; }

    up-to-date version can be found on my github for future reference

Re: process run in background (daemonize)
by tye (Sage) on Jul 27, 2016 at 20:10 UTC

    This is usually solved by having the script "daemonize" itself. It only requires a few steps and isn't really that complicated, but you might want to at least take a few minutes to examine some of the pre-rolled implementations, several of which can be found with the simple search: daemonize. (At my current job, we already had that particular bit of code already pre-rolled locally by somebody who predated my employment significantly, so I made significant improvements to that version rather than evaluating the ones on CPAN.)

    - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-12-04 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?