Display current directory for a running process for script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display current directory for a running process for script
# 1  
Old 07-27-2014
Computer Display current directory for a running process for script

I'm trying to create a ksh script to do the following:

1) Ask the user what process they want to search for.
2) Have the script read the input as a variable and use it to search for the process.
3) Display the current time & date, the working directory of the process, and finally display the information for only that process.

Ideal script output to the user:

current date here
Code:
1390:       /home2/TEST/
root 1390        1   0   May 10      - 11:02 phantom EBC

Here is how I would manually perform the task:
Code:
# ps -ef | grep EBC
    root 1390        1   0   May 10      - 11:02 phantom EBC
    root 1652        1   0   Apr 17      -  0:22 phantom EBC
    root 1884        1   0   Jun 04      -  4:43 phantom EBC
    root 1912        1   0   Jun 22      -  7:08 phantom EBC
    root 2314 3134   0 12:05:48  pts/3  0:00 grep EBC
    root 2698        1   0   Apr 17      - 10:51 phantom EBC

# procwdx 1390
1390:       /home2/TEST/

This is what I have so far but I know its not right, Please help Smilie
Code:
#!/bin/ksh

echo -n "Enter process name:"
read Answer
for i in `ps -ef | grep EBC | grep -v grep | awk '{print $2}'`;do
        procwdx $i | awk '{print $1 $Answer}'| ps -ef | grep $1
done

echo $(date +'%D %T')


Last edited by Scott; 07-27-2014 at 04:57 PM.. Reason: Please use CODE tags for output too, to preserve formatting
# 2  
Old 07-27-2014
How do you want to determine THE process? If the user enters "phantom EBC", there's five of them to select from... And, how do you call the script?
# 3  
Old 07-27-2014
If theres a way where the user enters TEST which would be the working directory for pid 1390.
# 4  
Old 07-27-2014
Here we only prompt for directory if more than 1 process matches string.

As your probably on AIX, grep will most likely not support -w if that's the case just use -q instead of -wq.

Code:
printf "Enter process name: "
read Answer

count=`ps -eo pid,comm | grep "$Answer" | wc -l`

if [ $count -gt 1 ]
then
   printf "\nMore that 1 process found\n"
   printf "Enter process folder: "
   read Folder
fi

ps -eo pid,comm | grep "$Answer" | while read pid command
do
   ln=`procwdx $pid`
   echo "$ln" | grep -wq "${Folder}" && echo "$ln"
done

# 5  
Old 07-27-2014
Thanks Chubler XL!
Yes I'm currently working on aix os. However, when I use the command ps -eo pid,comm it isnt returning anything.
# 6  
Old 07-27-2014
try ps -eo pid,cmd or ps -e -o pid,args
# 7  
Old 08-12-2014
Thank you Chubler_XL! I was able to get the script to work. Only having one little issue with my script reading the minus sign " - " as is and not an argument. Any suggestions will be greatly appreciated.

---------- Post updated at 04:54 AM ---------- Previous update was at 04:23 AM ----------

actually nevermind I found my mistake Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to display only the first 5 running process using top in shell scripting?

topfunc() { top } topfunc Here i used the top command inside a function,and i called the function. when executing this bash file i get all the process which are using by the kernel i just want to display only the first 5 running process. is it possible? (7 Replies)
Discussion started by: Meeran Rizvi
7 Replies

2. Shell Programming and Scripting

List Process running under current user

Hi, i need to list the processes running only under current logged in user. EX: $ whoami oraaqw $ ps -ef | grep tnslsnr oraaqw 11403300 19267592 0 09:14:47 pts/3 0:00 grep tnslsnr oraaqw 15794208 1 0 Jan 14 - 11:59... (6 Replies)
Discussion started by: aravindadla
6 Replies

3. Shell Programming and Scripting

Command to know all the Current running process and how to kill

All, 1.What is the unix comand used for all current running process (Including All current running processes Parent ->child->subchild process) 2.If child and subchild processes are running then what is the unix command to kill parent and its all child subchild processes in UNIX. Kindly... (7 Replies)
Discussion started by: skp
7 Replies

4. UNIX for Dummies Questions & Answers

Running different process from current process?

I have been having some trouble trying to get some code working, so I was wondering...what system calls are required to execute a different program from an already running process? (1 Reply)
Discussion started by: Midwest Product
1 Replies

5. UNIX for Advanced & Expert Users

how to display pid and other parameters of current process through kernel module ?

how to display pid and other parameters of current process in linux platform ? i know it can be done through a linux commmand ps -F but i want it done through kernel program thanks in advance (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

6. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

7. UNIX for Dummies Questions & Answers

Current Process Running.

Hi all, When I issued command ps -ef|grep Vinay in a UNIX machine, I got the following Vinay 22491 1 255 Jun 18 ? 294248:53 -sh Vinay 26628 1 255 Jun 18 ? 294237:33 -sh Could you tell me what all process is running ? Please explain each of the fields. Thanks... (4 Replies)
Discussion started by: coolbhai
4 Replies

8. Shell Programming and Scripting

current running process in shell

hi what is the shell programming code to know the number of processes currently running on the machine & information about those processes. Another one is the configuration and usage of the UNIX file system? requesting all for help. thanks (1 Reply)
Discussion started by: moco
1 Replies

9. Programming

to find current running process

Hi All, The scenario is like this: There is a process say "A" which create a child process say "B" if some condition is true and process "A" terminates. "B" invokes some C program say "C" using 'execl' function. The job of program "C" is to keep polling the server until the server will be up.... (2 Replies)
Discussion started by: ranjkuma692
2 Replies

10. UNIX for Advanced & Expert Users

how to make a current running process ignore SIGHUP signal?

I ask this question since sometimes i run a time-consuming ftp in foreground and forget to use nohup ftp.sh & to put this work background and can still running after i log off. Assume this ftp task have run 1 hour, and still 1 hour time to end, i don't want to abort the ftp, first, i use ctrl+Z... (3 Replies)
Discussion started by: stevensxiao
3 Replies
Login or Register to Ask a Question