DaemonForums  

Go Back   DaemonForums > Miscellaneous > General software and network

General software and network General OS-independent software and network questions, X11, MTA, routing, etc.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default Returning the PID of a ssh process

How do you get the PID of a backgrounded ('-f ') ssh process?
Reply With Quote
  #2   (View Single Post)  
Old 14th September 2015
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 5,934
Default

pgrep(1) should work.
Reply With Quote
  #3   (View Single Post)  
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

There will be other ssh instances running, I'd have to parse the command line arguments.

pgrep with '-x' and '-n' flags after pausing the script a few milliseconds is what I have been doing.

Last edited by gso; 14th September 2015 at 02:56 PM.
Reply With Quote
  #4   (View Single Post)  
Old 14th September 2015
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 5,934
Default

I was thinking parsing the output of $ pgrep -lf. Another option might be to write a program that calls getppid(2) or getpid(2).
Reply With Quote
  #5   (View Single Post)  
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

ssh -D 1080 -CNf -o ExitOnForwardFailure=yes user@host... && pgrep -x -n ssh

Seems to work quite reliably. Having said that there is only ever going to be one ssh process listening on 1080.
Reply With Quote
  #6   (View Single Post)  
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

ssh -D 1080 -CNf -o ExitOnForwardFailure=yes user@host... && pgrep -f 'ssh -D 1080'
Reply With Quote
  #7   (View Single Post)  
Old 14th September 2015
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 5,934
Default

Just tested a simple program. Use getppid(2).
Code:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int
main(int argc, char **argv) {
   printf("Process ID: %d\n", getppid());
   exit(0);
}
Reply With Quote
  #8   (View Single Post)  
Old 14th September 2015
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 5,934
Default

Ah. I did not understand that you wanted the PID from an external script -- my assumption was that you wanted the background process to learn its own PID -- perhaps to write into /var/run.
Reply With Quote
  #9   (View Single Post)  
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

For ssh not to have the usual command line option to write the PID to a file, there would have to be a reason?

Last edited by gso; 14th September 2015 at 06:15 PM.
Reply With Quote
Old 14th September 2015
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 5,934
Default

Without any knowledge of OpenSSH's requirements history, I can only surmise that you are the first person to need it.

There is the ControlMaster configuration option with ssh -O ctl_cmd which may be able to provide process governance you need?
Reply With Quote
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

ssh -O stop server1
ssh -O cancel -L 8080:localhost:80 -S ~/.ssh/controlmaster/fred@server.example.org:22 server.example.org

https://en.wikibooks.org/wiki/OpenSS...k/Multiplexing

There are however caveats:

http://www.anchor.com.au/blog/2010/0...-bad-the-ugly/

[Edit: if there were only one connection per socket then I can't see there would be any issues.]

Last edited by gso; 14th September 2015 at 04:56 PM.
Reply With Quote
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

[djm [at] demiurg ~]$ ssh -nNfS ~/ctl-sock-blah localhost
[djm [at] demiurg ~]$ ssh -S ~/ctl-sock-blah -O check localhost
Master running (pid=3517)
[djm [at] demiurg ~]$ ssh -S ~/ctl-sock-blah -O exit localhost
Exit request sent.
http://www.gossamer-threads.com/lists/openssh/dev/48040

By no means a new topic.
Reply With Quote
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

I am using ssh-agent at the moment anyway, '-f' isn't strictly necessary

ssh -D 1080 -CN user@host... &
echo $!
Reply With Quote
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

Still can't help wondering what the folks over at OpenSSH really had against the patch proposal, OpenBSD folk can be a bit odd at times (a reputation which doesn't seem to bother them) but never-the-less I don't think would act without good reason.
Reply With Quote
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

If you want to know if a process is still running OK (or likewise close a ssh connection) then a PID is not the way to do it, the control socket will do the job.

[Edit: the man page could be a bit more helpful, it's one thing having the tools, but the tools and the techniques and skills is actually what is needed.]

Last edited by gso; 14th September 2015 at 05:37 PM.
Reply With Quote
Old 14th September 2015
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 5,934
Default

The "folks" with this particular determination were just one: Damien Miller. He's the "djm" in the mailing list history you linked in post #12. He is still the lead developer for OpenSSH.

It's possible Damien may be willing to revisit the issue, if there is some reason the ControlMaster option cannot be used. You could always ask him.

I don't see the caveats as critical, because they are related to TCP session performance and master session control. Neither should be an issue if you enable ControlMaster and set unique ControlSockets for each background shell you wish to manage.

Another alternative is using a getppid(2) syscall from the background shell, as I'd originally envisioned you needed.
Reply With Quote
Old 14th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

If I submit a patch it will be to the man page.
Reply With Quote
Old 15th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

A rc script duly implementing the above http://daemonforums.org/showthread.php?t=9384

As to the man page, the '-O' flag is described as:
Code:
     -O	ctl_cmd
	     Control an	active connection multiplexing master process.	When
	     the -O option is specified, the ctl_cmd argument is interpreted
	     and passed	to the master process.	Valid commands are: ``check''
	     (check that the master process is running), ``forward'' (request
	     forwardings without command execution), ``cancel''	(cancel	for-
	     wardings),	``exit'' (request the master to	exit), and ``stop''
	     (request the master to stop accepting further multiplexing
	     requests).
I would be tempted to add something along the lines of the following to the end of the paragraph:

Quote:
The ''check'' command can be used to obtain the PID of a running master process. All output from commands is sent to standard error.
Reply With Quote
Old 16th September 2015
gso gso is offline
Port Guard
 
Join Date: Nov 2014
Posts: 35
Default

I think I see your point about using the ppid:

Code:
pgrep -P $$ -x ssh
A process timestamp can be cached also:

Code:
ps -o lstart= -p PID
However assuming a control socket can be closed following reading the PID then this might be a preferable approach for some people.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenBSD 5.7 Update Process Peter_APIIT OpenBSD General 8 9th September 2015 01:53 PM
PhotoRec process bug LeFrettchen General software and network 4 1st July 2014 11:15 PM
returning newbie questions jimbus FreeBSD Installation and Upgrading 6 22nd June 2012 02:19 AM
Help with pthread_create not returning Darwimy Programming 1 16th December 2010 07:07 PM
Loader, MBR and the boot process Jago FreeBSD Installation and Upgrading 1 22nd January 2010 04:52 AM


All times are GMT. The time now is 03:06 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick