OpenSSH에는 LocalCommandssh 연결을 작성할 때 클라이언트 측에서 명령을 실행 하는 옵션이 있습니다. 불행히도 ssh 세션이 설정 되기 전에 명령이 실행됩니다 . 그러나 그것은 나에게 어떻게 든 ssh 세션이 끝나기를 기다리는 이전 프로세스를 얻을 수 있다는 아이디어를주었습니다. ssh 프로세스가 LocalCommand의 상위 PID라는 사실에도 불구하고 여전히 쉽지는 않습니다.
그러나 MacOS X에서 나에게 맞는 것을 발견했으며 Linux가 아닌 경우 (다른) BSD에서 작동해야합니다. kqueue()인터페이스를 사용하여 자체 ppid를 기다린 다음 프로세스가 종료되면 제공된 명령을 실행 하는 작은 C 프로그램을 작성했습니다 . (아래 관심있는 사람들을위한 소스 코드 목록)
이제 ~/.ssh/config파일 에서이 프로그램을 참조하면 됩니다.
host hp-switch*
PermitLocalCommand yes
LocalCommand ~/bin/wait4parent 'tput smam'
그리고 이것은 잘 작동하는 것 같습니다. 리눅스에서 당신의 사람들 ... 나는 당신이 같은 LocalCommand바보 를 조사하여 같은 종류의 것을 시도 할 수 있다고 생각합니다. 그리고 그 pid가 재사용되지 않기를 바랍니다. ( /programming/1157700/how-to-wait-for-exit-of-non-children-processes 참조 )
wait4parent.c :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
int main(int argc, char **argv) {
pid_t ppid, fpid;
struct kevent kev;
int kq;
int kret;
struct timespec timeout;
if ( argc > 2 ) {
fprintf(stderr, "Please quote the command you want to run\n");
exit(-1);
}
ppid = getppid();
fpid = fork();
if ( fpid == -1 ) {
perror("fork");
exit(-1);
}
if ( fpid != 0 ) {
exit(0);
}
EV_SET(&kev, ppid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, 0);
kq = kqueue();
if ( kq == -1 ) {
perror("kqueue");
exit(-1);
}
kret = kevent(kq, &kev, 1, NULL, 0, NULL);
if ( kret == -1 ) {
perror("kevent");
exit(-1);
}
timeout.tv_sec = ( 8 /*hours*/ * 60 /*minutes per hour*/ * 60 /*seconds per minute*/ );
timeout.tv_nsec = 0;
kret = kevent(kq, NULL, 0, &kev, 1, &timeout);
if ( kret == -1 ) {
perror("kevent");
exit(-1);
}
if ( kret > 0 ) {
system(argv[1]);
}
/* ( kret == 0 ) means timeout; don't do anything */
exit(0);
}