Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a list of servers 20+ that I would like to get the shell that they are using. While logged onto a server I can run the following command

$ ps -p $$
PID TTY          TIME CMD
12022 pts/6    00:00:00 bash

Instead of touching each server and doing this I tried to loop it with a list

$ for i in `cat servlist`; do echo $i ; ssh $i ps -p $$ ; done
serv1
PID TTY          TIME CMD
serv2
PID TTY          TIME CMD
serv3
PID TTY          TIME CMD

Looping this does not show the expected output. I then tried to ssh to a single server and run the command but got the same error.

$ ssh serv4 ps -p $$
PID TTY          TIME CMD

Why is this happening, nothing jumps out at me in the ssh man pages.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

That is because the process does not exist.

The $$ is being evaluated locally, and all servers are being passed the same number. A number that is not a currently used PID on the servers.

All the $ stuff is done by the shell, not the commands. You need to escape it, so that it is evaluated by the shell on the server.

Try \$\$


e.g.

for i in $(cat servlist); do echo $i; ssh $i 'ps -p $$'; done

untested

share|improve this answer
    
I did not think to look more into the ps -p $$ command. When running for i in 'cat servlist'; do echo $i ; ssh $i ps -p \$\$ serv1 PID TTY TIME CMD 7199 ? 00:00:00 ps serv2 PID TTY TIME CMD 13842 ? 00:00:00 ps It did just occur to me to cat /etc/passwd | grep user" and find the default shell that way. –  Jon Wright Jun 22 at 21:41
1  
@JonWright getent passwd | awk -F: '$1=="user" {print $7}' will show you the login shell for "user". Is that what you wanted? –  roaima Jun 22 at 21:50
    
@roaima yes this is what I wanted. From habit I wanted to use ps -p $$. –  Jon Wright Jun 22 at 21:59
    
Top tip. use $(command) instead of (I can not type this) using back ticks. –  richard Jun 22 at 22:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.