How to get PID of started process in csh (not bash!)

5.7k Views Asked by At

For bash it is:

command; echo $!

but it doesn't work for csh (in Unix Tru64, no bash there). How can I get the PID of arbitrary started process?

2

There are 2 best solutions below

1
On BEST ANSWER

Use % pidof command, or % ps.

Example usage:

$ csh
% sleep 420 &
[1] 28147
% pidof sleep
28147
% sleep 250 &
[2] 28154
% pidof sleep
28154 28141
% ps
PID TTY          TIME CMD
28059 pts/5    00:00:00 bash
28146 pts/5    00:00:00 csh
28147 pts/5    00:00:00 sleep
28154 pts/5    00:00:00 sleep
28157 pts/5    00:00:00 ps

You can easily pipe the output of the ps command to get the line and PID you need. The last started process will most likely be on line four (including header) at any time. Unless, that is, you immediately start the c-shell rather than entering it from bash, in that case it would be on line three.

Example:

$ csh
% sleep 420 &
[1] 28441
% ps | awk 'NR == 4 {print $1}'
28441
0
On

Use $$ in csh to get the current process ID.

Example:

% echo $$

25352