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

Hi,

I am writing a perl script, which will execute a command in Linux box. The command should run in background. So, the command should run with '&'. The command should be --

./startWebLogic.sh &

My perl command is looking something like this --

my @lines = qx/"D:\\plink -ssh $myhash{ROLE_ACCNT}\@$myhash{OSESSRVRS} -i $myhash{KEY} -t $myhash{OSES_HOME}\/startWebLogic.sh \&"/;

But on running, the control is not coming back to perl. On looking in Linux box for the command, it shows that & is missing '&' from the command that got executed. In Linux box, it looks something like this --

65503 9425 9424 0 01:33 pts/1 00:00:00 -sh -c /mis/startWebLo +gic.sh 65503 9459 9425 0 01:33 pts/1 00:00:00 /bin/sh /mis/startWebL +ogic.sh

Please help me, in finding a solution. How to get the command to run in background.

Thanks, Partho

Replies are listed 'Best First'.
Re: Control not coming back to perl script
by salva (Canon) on Mar 03, 2016 at 12:57 UTC
    The remote sshd process is not going to end the connection until all the subprocesses launched close their stdio streams.

    Try redirecting stdout to /dev/null on the remote side:

    qx/"D:\\plink -ssh $myhash{ROLE_ACCNT}\@$myhash{OSESSRVRS} -i $myhash{ +KEY} -t $myhash{OSES_HOME}\/startWebLogic.sh >\/dev\/null \&"/;
Re: Control not coming back to perl script
by Tanktalus (Canon) on Mar 03, 2016 at 14:49 UTC

    I'm confused as to what you're trying to accomplish here. You want the output of this command (which means waiting until the output is closed) or to run in the background? You can't do both - at least not with qx. If you want the application to run in the background while you continue, you will, by definition, not get any lines back immediately. You can do this with proper threading or event handling, but you're still not going to get lines immediately. I'm not even sure that you really want the lines, but you're specifically asking for it, so I'm assuming there's a reason why you're using qx instead of system.

    Further, I'm confused by those quotes inside the qx. That doesn't make any sense to me whatsoever. If the open double-quote was after the -i, that'd make more sense.

    Another option, if you don't need the output, is to use system(1, $cmd) - on Windows, that should start the command in the background, though it'll leave the connection open.

    I'd suggest using nohup and redirecting output (the latter which salva suggested). And maybe look into whether plink has some of the ssh options that force its connection into the background.

Re: Control not coming back to perl script
by parthodas (Acolyte) on Mar 15, 2016 at 11:09 UTC

    Hi,

    Thanks for the reply.

    I have not tried routing the output to null, > /dev/null. I'll test it from my side and update later.

    I thought that the control might not return. So, I put all the commands in temp file, $mycmdfile. Then I used batch mode to run the command. It looks something like this --

    my @lines = qx/"D:\\plink -ssh $myhash{ROLE_ACCNT}\@$myhash{OSESSRVRS} -i $myhash{KEY} -t -batch < $mycmdfile"/;

    Thanks, Partho