use Net::OpenSSH; my $account_id = 1; my $ctl_path = "/.../some/known/place/to/put/the/control/path-$account_id"; while (1) { eval { unlink $ctl_path; my $ssh = Net::OpenSSH->new($host, ..., ctl_path => $ctl_path); $ssh->die_on_error("Unable to connect to remote host"); while (1) { # run the dummy command $ssh->sftp->stat("/") or die "SFTP command failed"; sleep 10; } }; warn $@ if $@; warn "delaying before restarting SSH connection\n"; sleep 5; }