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

Hello guys, I want to run some process in the background even after script exit. how do i achieve this?

#!/usr/bin/perl // do some job. `ssh remotehost "sleep 30000"`; /backround job should run even after s +cript exit. //contiue job execution exit;

Replies are listed 'Best First'.
Re: How to run process in the backround even after perl script exit?
by 1nickt (Canon) on Apr 10, 2020 at 16:27 UTC

    Hi, welcome to the Monastery.

    "Running in a restricted environment server, I can't install any SSH module support."

    That's not something I would accept. Are you at work? Are you on a shared server? Consider getting a slice on a Linode or AWS so you can develop in Perl with a full deck of cards.

    For a cheap solution to your problem, maybe you could fork a child process then exec the shell job from the child.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: How to run process in the backround even after perl script exit?
by Corion (Patriarch) on Apr 10, 2020 at 16:39 UTC

    See the nohup program and the & shell "operator" (or whatever they are called).

    This is the same as outside of Perl.

      can you post some sample codes?

      it's not working: `ssh -o StrictHostKeyChecking=No $user\@$hostname 'nohup sleep 100&'`;
Re: How to run process in the backround even after perl script exit?
by hippo (Archbishop) on Apr 11, 2020 at 10:07 UTC
    `ssh remotehost "sleep 30000"`;

    Never use backticks if you do nothing with the captured output. Use system instead.

    $ date; perl -e 'system q{nohup ping -c 5 perlmonks.org &};'; date; sl +eep 10; date; cat nohup.out Sat 11 Apr 11:06:34 BST 2020 nohup: appending output to ‘nohup.out’ Sat 11 Apr 11:06:34 BST 2020 Sat 11 Apr 11:06:44 BST 2020 PING perlmonks.org (209.197.123.153) 56(84) bytes of data. 64 bytes from perlmonks.com (209.197.123.153): icmp_seq=1 ttl=57 time= +96.2 ms 64 bytes from perlmonks.com (209.197.123.153): icmp_seq=2 ttl=57 time= +95.5 ms 64 bytes from perlmonks.com (209.197.123.153): icmp_seq=3 ttl=57 time= +95.1 ms 64 bytes from perlmonks.com (209.197.123.153): icmp_seq=4 ttl=57 time= +94.5 ms 64 bytes from perlmonks.com (209.197.123.153): icmp_seq=5 ttl=57 time= +95.1 ms --- perlmonks.org ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4015ms rtt min/avg/max/mdev = 94.518/95.322/96.248/0.563 ms $