2

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.

4

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

  • 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. – SpruceTips Jun 22 '15 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 '15 at 21:50
  • @roaima yes this is what I wanted. From habit I wanted to use ps -p $$. – SpruceTips Jun 22 '15 at 21:59
  • Top tip. use $(command) instead of (I can not type this) using back ticks. – ctrl-alt-delor Jun 22 '15 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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