ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using Perl, I would like to start an SSH tunnel and then run vncviewer to connect the port. I understand I can do so very simply with the following command:
system("ssh -l <username> -L 10000:localhost:5901 <remote host>");
vncviewer localhost:10000
But then the process does not execute the vncviewer command.
Any idea? I think I need to do something using either threading or fork. But I am not sure how I can do so with either.
yes you need to thread or fork, the system call doesn't return until the process it is running has finished when is why vncviewer command isn't running.
Code:
$not_yet = 1;
$pid = fork();
if ($pid == 0) {
$not_yet = 0;
system("ssh -l <username> -L 10000:localhost:5901 <remote host>");
exit(0);
} elsif ($pid > 0) {
while ($not_yet == 1) {
sleep(1);
}
sleep(5); # not a guarantee that the ssh has already run
# really should check the output of the vncviewer and react to error conditions
system("vncviewer localhost:10000");
} else {
print "fork failed\n";
}
Last edited by estabroo; 11-05-2008 at 09:47 PM.
Reason: accidentally hit return early
Okay, I tried your solution, but ssh is still having control over the process. Basically, when i run
system("ssh -l <username> -L 10000:localhost:5901 <remote host>");
It brings me into the console of the ssh server, and vncviewer does not load.
yes remove the $not_yet references. I'd forgotten that in perl variable space isn't shared on a fork()
Code:
$pid = fork();
if ($pid == 0) {
system("ssh -l <username> -L 10000:localhost:5901 <remote host>");
exit(0);
} elsif ($pid > 0) {
sleep(5); # not a guarantee that the ssh has already run
# really should check the output of the vncviewer and react to error conditions
system("vncviewer localhost:10000");
} else {
print "fork failed\n";
}
I just realised that this solution works but if i were to exit the program (i.e. close vncviewer), the processes are still being run in the background. Are there any ways I can kill the child process and the parent process?
I understand I have to use something like signals. Are there weres that I don't have to use signals. If i have to, do you mind guiding me a little on the implementation of it?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.