Getting PID of process in Shell Script
Just grep away grep itself!
process_id=`/bin/ps -fu $USER| grep "ABCD" | grep -v "grep" | awk '{print $2}'`
How to return the PID from a bash script executed from a bash script
we use pgrep to get the pid of a processm like below
PID=$(pgrep -f "daemon_itinerary.sh" | xargs)
# xargs - is given because pgrep will return both process id as well as parent pid
# also it will help us to get all pids if multiple instances are running.
# pgrep option to get session id or parent id alone, here its from manual
# -P, --parent ppid,...
# Only match processes whose parent process ID is listed.
# -s, --session sid,...
# Only match processes whose process session ID is listed. Session ID 0 is translated into pgrep's or pkill's own session ID.
Shell script to capture Process ID and kill it if exist
Actually the easiest way to do that would be to pass kill arguments like below:
ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill
How can a Linux/Unix Bash script get its own PID?
The variable $$
contains the PID.
How to know the process id of the process which executes a given script?
Adding echo $PPID
in the script works as suggested by @anubhav
How to find the PID of a running command in bash?
Thanks to @glennjackman I managed to get the PID I wanted with a simple pgrep search_word
. At first it wasn't working, but somehow I made it work after some trial and error. For those wanting the PID on a variable, just type
pid=$(pgrep search_word)
Regarding the problem with screen -ls
not showing my detached session, it's still not solved, but I'm not bothered with it. Thanks again for solving my problem @glennjackman !
EDIT:
Second problem solved, check the comments on berends answer.
Shell Script:How to get all the Process Id which are using a file-system/directory
If you only want id
instead of id(user)
then don't use the -u
option. Documentation of fuser -u
:
-u, --user
Append the user name of the process owner to each PID.
For me, fuser -c /
has a different format than your sample. Each id is followed by letters denoting the type of access. The letters are printed to stderr
, therefore I will use 2>&-
to hide them.
$ fuser -c /
/: 1717rce 1754rce 1765rce 1785rce ...
$ fuser -c / 2>&-
1717 1754 1765 1785 ...
You can use grep
to print one id per line:
$ fuser -c / 2>&- | grep -o '[0-9]*'
1717
1754
1765
1785
...
However, to run a loop you don't need one id per line. Ids separated by spaces work as well:
for id in $(fuser -c / 2>&-); do
echo "id = $id"
done
how to extract the PID of a process by command line
Just use pidof
, rather to use other commands and apply post-processing actions on them.
$ pidof cron
22434
To make the command return only one PID
pertaining to to the process, use the -s
flag
-s
Single shot - this instructs the program to only return one pid.
Related Topics
Spawn_Id: Spawn Id Exp6 Not Open
Linux - Bash Redirect a String to a File
Perl Script to Capture Stderr and Stdout of Command Executed in Back-Quotes
Mongodb Service Fails After Reboot
Grep,How to Search for Exact Pattern
Passing a Command with Arguments as a String to Docker Run
Starting with Kde Frameworks 5 and Qt Creator
Which Tool Has Replaced Gatttool in Bluez5
Launching Sonar Scanner from a Gitlab Docker Runner
How to Rename Files in Zip Archive Without Extracting and Recompressing Them
Does Zgrep Unzip a File Before Searching
Cuda-Gdb Not Working in Nsight on Linux
Container Running in Privileged Mode
Maximum Number of Threads Allowed to Run
Difference Between The Commands "Gcloud Compute Ssh" and "Ssh"