How to find PID of an ongoing job in UNIX?

11,456

Solution 1

you can also consider do use

pgrep "you_script"

or

pgrep -f "complete_cmd_line_of_script"

"process-grep" results only the PID

If you want to kill the process you found, you also can use pkill, which uses the same syntax as pgrep

Solution 2

ps aux | grep "your_script_name.sh"
Share:
11,456

Related videos on Youtube

ursitesion
Author by

ursitesion

Updated on September 18, 2022

Comments

  • ursitesion
    ursitesion over 1 year

    I executed a shell script which will call some internal applications and will take few hours. Due to some problems, I have to stop or kill that job. For this, first of all, I ran ps -u $uname to list all the ongoing jobs.

    PID TTY         TIME CMD
     14172 pts/1       0:00 ps
     12939 ?           0:00 sshd
     12904 ?           0:01 sshd
     13132 pts/1       0:00 bash
     12945 pts/1       0:00 sh
     12897 ?           0:00 sshd
    

    Now, how will I find correct PID to kill?

    • mnille
      mnille about 8 years
      If you add the option -f (full) you get more details about the command (CMD). With that information you may be able to do a grep.
    • ursitesion
      ursitesion about 8 years
      What is the wrong with this question which tends people to downvote?
  • Арсений Черенков
    Арсений Черенков about 8 years
    you should pipe grep, grep and awk ! If yourscript is a fixed string 1) use | grep [y]ourscript (this avoid a |grep -v grep), 2) then for awk, use ps aux | awk '/[y]ourscrip/ { print $2} '
  • Colonel Beauvel
    Colonel Beauvel about 8 years
    you mean 'shoud not' if I understand? Thanks for the tip
  • Арсений Черенков
    Арсений Черенков about 8 years
    yes, thou shall not