2014-09-25 57 views
0

开始对我的英语道歉。如何检查正在运行的进程的路径?

我有一个正在运行的进程在服务器上,当我执行:

ps -aux | grep script.sh 

我得到了这样的结果:

root  28104 0.0 0.0 106096 1220 pts/7 S+ 08:27 0:00 /bin/bash ./script.sh 

但是这个剧本是从运行如。 /home/user/my/program/script.sh

所以,我怎样才能从那里剧本正在运行的完整路径?我有许多脚本,其名称完全相同,但它们来自不同的位置,我需要知道给定脚本的运行位置。

感谢您的回复!

回答

2

尝试下面的脚本:

for each in `pidof script.sh` 
do 
    readlink /proc/$each/cwd 
done 

这会发现所有script.sh脚本的运行pid.s,找到的/ proc相应的CWD(当前工作目录)。

1

使用pwdx 用法:pwdx PID ... (表演过程中工作目录) 例如,

pwdx 20102 

,其中20102是PID 这将显示该进程的进程工作目录

+0

无法使用您的命令创建一个脚本 – Ram 2014-09-25 13:21:23

0
#!/bin/bash 

#declare the associative array with PID as key and process directory as value 
declare -A dirr 

#This will get the pid of the script 
pid_proc=($(ps -eaf | grep "$1.sh" | grep -v "grep" | awk '{print $2}')) 


for PID in ${pid_proc[@]} 
do 
    #using Debasish method 
    dirr[$PID]=$(pwdx $PID) 
    # Below are different process to get the CWD of running process 
    # using user1984289 method 
    #dirr[$PID]=$(readlink /proc/"$PID"/cwd) 
    #dirr[$PID]=$(cd /proc/$PID/cwd; /bin/pwd) 
done 

# iterate using the keys of the associative and get the working directory 
for PID in "${!dirr[@]}" 
do 
echo "The script '$1.sh' with PID:'$PID' is in the directory '${dirr[$PID]}'" 
done 
0

使用pgrep获取实例的PID,然后阅读关联的CWD目录的链接。基本上,同样的做法@ user1984289但使用的pgrep代替pidof不我的系统上(甚至与-x选项)匹配的bash脚本名称:

for pid in $(pgrep -f foo.sh); do readlink /proc/$pid/cwd; done 

只要改变foo.sh到脚本的名称。

相关问题