Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Remotely Executing a background command

by NolanPL (Novice)
on Jul 31, 2008 at 13:40 UTC ( #701418=perlquestion: print w/replies, xml ) Need Help??

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

I have a function in place which ssh's into a remote machine and executes a command, this has worked great, but now I have a process that has to be run in the background on the remote machine, and the function waits on the return value of the program like any system call does. I send the command to the remote system as system("ssh root@ip $command") where command is of the sort ./sleepwake (args) & The command executes, but I need it to return a value and continue running. Any ideas?
  • Comment on Remotely Executing a background command

Replies are listed 'Best First'.
Re: Remotely Executing a background command
by BrowserUk (Patriarch) on Jul 31, 2008 at 14:21 UTC

    Use an inline thread:

    use threads; ... my $pid :shared; my $tid = async{ ## Removed 'my' below. Thanks Jettero. $pid = system("ssh root@ip ./sleepwake (args) &"); $pid = 0; return $?; }; ... ## kill 2, $pid if $pid; ... unless( $pid ) my $result = $tid->join; ## do something; } ...

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Remotely Executing a background command
by moritz (Cardinal) on Jul 31, 2008 at 13:51 UTC
    Returning a value and running in the background seems contradictory to me. If you send a job to the background you're explicitly not waiting for a result.

    For running something in the background on the remote host after your ssh connection has terminated, take a look at nohup and screen.

      ...and if you want the status code of a background command, use wait(1).

      HTH more :-)

      At last, a user level that overstates my experience :-))
Re: Remotely Executing a background command
by starX (Chaplain) on Jul 31, 2008 at 13:53 UTC
    Why not fork off a child process after confirming your ssh connection is successful. Have the child process execute the command you need to check the return value of, and then signal the parent that it (the child) was successful, and that the parent is free to do its thing.

    Disclaimer, I've never done this in Perl before, but it works splendidly in C. On POSIX compliant systems, at least. You might want to give perlipc a read if you think this interprocess communication route might be the way for you to go.

Re: Remotely Executing a background command
by massa (Hermit) on Jul 31, 2008 at 14:41 UTC

    from man ssh:

    Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 programs at a remote site is with something like ssh -f host xterm.


    system ssh => 'root@ip' => $command
    ... and you can drop the "&" in $command.
    []s, HTH, Massa (κς,πμ,πλ)
Re: Remotely Executing a background command
by tilly (Archbishop) on Jul 31, 2008 at 23:42 UTC
    Let me describe your problem to see if I correctly understand what is going on.

    Your problem is that you want to run a program on another machine, and you want your program to not wait for it. What you're doing is sshing into that machine, running your command in the background, and exiting. Your problem is that the ssh shell stubbornly will not return until the subprocess is all done, which means that you're waiting for the process to finish, which is what you didn't want.

    If that is what is happening, then the simplest solution is to use the at command in this form.

    ssh root@ip 'echo $command | at now'
    This will queue up the job to run using the batch system immediately. Your ssh session should return immediately. Depending on how cron has been configured, any output likely comes to someone in email.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2023-12-02 04:31 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (13 votes). Check out past polls.