linux shell 脚本查找进程cwd 求解

stephen2017
  • 12
root@center1:~/douyin/DYSECOO# ps -ef|grep msg_test
root     30228 29891  2 14:02 pts/4    00:00:40 python3 msg_test.py

上面是通过linux 命令查找得到的进程,然后通过进程查找得到下面的,我想要cwd 这个目录 ,linux 脚本如何写呢?

root@center1:~/douyin/DYSECOO# ll /proc/30228
total 0
dr-xr-xr-x   9 root root 0 Feb 24 14:02 ./
dr-xr-xr-x 248 root root 0 Feb  5 18:48 ../
dr-xr-xr-x   2 root root 0 Feb 24 14:28 attr/
-rw-r--r--   1 root root 0 Feb 24 14:28 autogroup
-r--------   1 root root 0 Feb 24 14:28 auxv
-r--r--r--   1 root root 0 Feb 24 14:28 cgroup
--w-------   1 root root 0 Feb 24 14:28 clear_refs
-r--r--r--   1 root root 0 Feb 24 14:03 cmdline
-rw-r--r--   1 root root 0 Feb 24 14:28 comm
-rw-r--r--   1 root root 0 Feb 24 14:28 coredump_filter
-r--r--r--   1 root root 0 Feb 24 14:28 cpuset
lrwxrwxrwx   1 root root 0 Feb 24 14:28 cwd -> /root/douyin/DYSECOO/
-r--------   1 root root 0 Feb 24 14:28 environ
lrwxrwxrwx   1 root root 0 Feb 24 14:28 exe -> /usr/local/bin/python3.7*
dr-x------   2 root root 0 Feb 24 14:03 fd/
dr-x------   2 root root 0 Feb 24 14:03 fdinfo/
-rw-r--r--   1 root root 0 Feb 24 14:28 gid_map
-r--------   1 root root 0 Feb 24 14:28 io
-r--r--r--   1 root root 0 Feb 24 14:28 latency
-r--r--r--   1 root root 0 Feb 24 14:28 limits
-rw-r--r--   1 root root 0 Feb 24 14:28 loginuid
dr-x------   2 root root 0 Feb 24 14:28 map_files/
-r--r--r--   1 root root 0 Feb 24 14:28 maps
-rw-------   1 root root 0 Feb 24 14:28 mem
-r--r--r--   1 root root 0 Feb 24 14:28 mountinfo
-r--r--r--   1 root root 0 Feb 24 14:28 mounts
-r--------   1 root root 0 Feb 24 14:28 mountstats
dr-xr-xr-x   5 root root 0 Feb 24 14:28 net/
dr-x--x--x   2 root root 0 Feb 24 14:28 ns/
-r--r--r--   1 root root 0 Feb 24 14:28 numa_maps
-rw-r--r--   1 root root 0 Feb 24 14:28 oom_adj
-r--r--r--   1 root root 0 Feb 24 14:28 oom_score
-rw-r--r--   1 root root 0 Feb 24 14:28 oom_score_adj
-r--r--r--   1 root root 0 Feb 24 14:28 pagemap
-r--r--r--   1 root root 0 Feb 24 14:28 personality
-rw-r--r--   1 root root 0 Feb 24 14:28 projid_map
lrwxrwxrwx   1 root root 0 Feb 24 14:28 root -> //
-rw-r--r--   1 root root 0 Feb 24 14:28 sched
-r--r--r--   1 root root 0 Feb 24 14:28 schedstat
-r--r--r--   1 root root 0 Feb 24 14:28 sessionid
-r--r--r--   1 root root 0 Feb 24 14:28 smaps
-r--r--r--   1 root root 0 Feb 24 14:28 stack
-r--r--r--   1 root root 0 Feb 24 14:03 stat
-r--r--r--   1 root root 0 Feb 24 14:28 statm
-r--r--r--   1 root root 0 Feb 24 14:27 status
-r--r--r--   1 root root 0 Feb 24 14:28 syscall
dr-xr-xr-x   3 root root 0 Feb 24 14:28 task/
-r--r--r--   1 root root 0 Feb 24 14:28 timers
-rw-r--r--   1 root root 0 Feb 24 14:28 uid_map
-r--r--r--   1 root root 0 Feb 24 14:28 wchan
回复
阅读 883
2 个回答
#!/bin/bash
pids=`ps -ef|grep $1|grep -v grep|awk '{print $2}'`
for pid in $pids
do
  echo "/proc/$pid/cwd"
done

保存为test.sh, chmod +x test.sh, 运行./test.sh msg_test

接个管道,然后
grep 'cwd'|awk -F'/' '{print $(NF-1)}'

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏