Reputation: 1608
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?
Upvotes: 1
Views: 6061
Reputation: 41
Use $$ in csh to get the current process ID.
Example:
% echo $$
25352
Upvotes: 4
Reputation: 4551
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
Upvotes: 2