Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
-1
votes
1
answers
72
views
Why a process can own 3 names in Linux?
We migrated an app, changed its name in `Makefile` from `flex_camera` to `flex_camera_lucid`. After deploying it to the target board(the original app is removed), as shown in the following screenshot, we get 3 different names of one process! But why? For the original app `flex_camera`, we can find i...
We migrated an app, changed its name in
Makefile
from flex_camera
to flex_camera_lucid
.
After deploying it to the target board(the original app is removed), as shown in the following screenshot, we get 3 different names of one process! But why?
For the original app flex_camera
, we can find it by ps -e
or pgrep
. But for the migrated app, neither ps -e | grep flex_camera_lucid
nor pgrep flex_camera_lucid
works. So, why?
Finally,what are the determinations of the name of a process in Linux ?
---
Makefile:
TARGETS = flex_camera_lucid
#...
.PHONY: default
default: $(TARGETS)
$(TARGETS): $(OBJS)
$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
Linux console screenshot:

Keelung
(155 rep)
Jun 20, 2025, 08:06 AM
• Last activity: Jun 20, 2025, 12:32 PM
2
votes
1
answers
1459
views
How to get a list of process ids of long command names, not command line parameters?
Since there's a 15-character limit on the length of process names in pgrep, I've been using `pgrep -f` to give me a list of process id's of running commands with longer names. However, it includes the full command line parameters, and this gives wrong results. For example: suppose `my_long_script_na...
Since there's a 15-character limit on the length of process names in pgrep, I've been using
pgrep -f
to give me a list of process id's of running commands with longer names. However, it includes the full command line parameters, and this gives wrong results.
For example: suppose my_long_script_name.sh
is running while I vi my_long_script_name.sh
(ignore editing a running program for the moment).
If I run pgrep -f my_long_script_name.sh
, it returns the process id of both my_long_script_name.sh
and vi my_long_script_name.sh
.
How can I get a list of process id's (that I can feed to kill in a list, the same way pgrep -d ' '
makes them) that are based only on process names not parameter lists?
I'm running Buster but see the same problem in earlier OS's such as Wheezy. As requested, if I run sed -n l "/proc/$pid/comm"
, where $pid
is $BASHPID
of my terminal session, I get bash$
. If I run sed -n l "/proc/$pid/cmdline"
, I get -bash\000$
. That's a lower case L, not a one, after -n
. If I run cat -vte
, it hangs.
NewtownGuy
(41 rep)
Nov 21, 2020, 01:06 PM
• Last activity: Mar 20, 2024, 07:16 PM
1
votes
1
answers
378
views
Why was pidof and pgrep removed from OpenSuse 15.5?
In openSuse leap 15.5, there are no packages offering pidof? SysVinit-tools package were part of the OS bundle previosuly, but was removed in 15.5. So, is there any alternates without installing sysVinit package (or) need to install the packages manually? Also any other distros as well planning to r...
In openSuse leap 15.5, there are no packages offering pidof?
SysVinit-tools package were part of the OS bundle previosuly, but was removed in 15.5. So, is there any alternates without installing sysVinit package (or) need to install the packages manually?
Also any other distros as well planning to remove the package(sysvinit-tools)?
Jagan
(11 rep)
Jan 23, 2024, 01:36 PM
• Last activity: Jan 23, 2024, 02:13 PM
2
votes
1
answers
54
views
Why does "pgrep -O 600" fail in an LXC? procps bug?
Debian 12.2 in an unpriviledged LXC (proxmox). It's almost 11:45 AM local time. At 5:00 AM in the morning, cron started a script: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND jan 26633 0.0 0.0 8500 2056 ? S 05:00 0:00 /usr/sbin/CRON -f I'm using `pgrep -f CRON -O 600` and I expect pgrep to...
Debian 12.2 in an unpriviledged LXC (proxmox). It's almost 11:45 AM local time. At 5:00 AM in the morning, cron started a script:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
jan 26633 0.0 0.0 8500 2056 ? S 05:00 0:00 /usr/sbin/CRON -f
I'm using
pgrep -f CRON -O 600
and I expect pgrep to return PID 26633, because the process is way older than 600 seconds. But pgrep returns nothing. If I leave out the -O
, it correctly returns the PID.
Doing the same on the host machine, i.e. outside of the LXC, it works correctly.
As pgrep uses procps, I looked there.
ps -o etime -p $pid
in the LXC: 441077225-02:04:48
(wrong, because since 5:00, ~6:45h passed)
ps -o etime -p $pid
on the host: 06:43:29
(correct)
Would that be a bug in procps or does it rather have to do with LXC?
Jan
(7962 rep)
Nov 30, 2023, 10:47 AM
• Last activity: Dec 1, 2023, 01:09 AM
-2
votes
1
answers
618
views
How to find one of multiple processes with the same name using pgrep (or another tool)?
I have several bash scripts running. The only thing that differs between them is the pid. I want to write a script like [this][1] that only monitors one specific bash process and exclude any daughter processes. I have seen [this][2] question but in that case the process names differs and you just ha...
I have several bash scripts running. The only thing that differs between them is the pid. I want to write a script like this that only monitors one specific bash process and exclude any daughter processes. I have seen this question but in that case the process names differs and you just have to write a precise regular expression.
Currently, if I do
% pgrep bash
40583
47095
48133
49244
and if I do
% pgrep -P 47095
47099
50151
I want to do something like
% pgrep bash -P 47095
and then get the result
47095 # (i.e. no daughter processes)
or an empty return value if 47095 no longer exists. How can this be achieved? The answer doesn't need to be based on pgrep, what is important is that it only returns one line if the process is running and nothing if there is no such process.
d-b
(2047 rep)
Jul 24, 2023, 12:57 PM
• Last activity: Jul 25, 2023, 04:43 AM
1
votes
0
answers
77
views
Why does pgrep return randomly a wrong pid?
In the past I used pid-files to guarantee race-condition safe execution of scripts. But this had the downside, that the pid-file was not deleted if the kernel killed the script somehow. So sometimes manual interaction was needed (remove the pid file, killing some sub processes). Finally I came up to...
In the past I used pid-files to guarantee race-condition safe execution of scripts. But this had the downside, that the pid-file was not deleted if the kernel killed the script somehow. So sometimes manual interaction was needed (remove the pid file, killing some sub processes).
Finally I came up to a solution which checks if a script is running by using
pgrep
and kills really old processes that shouldn't exist anymore:
while read -r pid cmd; do
# check if pid or parent pid belong to current script execution
if [[ $pid == "$$" ]] || [[ $(ps -o ppid= -p "$pid" | xargs) == "$$" ]]; then
continue
fi
# avoid re-execution of script within 12 hours (43200 seconds)
if [[ $(date +%s --d="now - $( stat -c%X "/proc/$pid" ) seconds") -lt 43200 ]]; then
echo "Error: Script is already running!"
exit
fi
# kill outdated script executions
if kill -9 "$pid"; then
echo "Warning: Outdated script execution ($pid) has been killed!"
fi
done /dev/null; then
continue
fi
# verify that pid is part of the list ("pstree --show-parents" returns list, even pid does not exist https://github.com/acg/psmisc/issues/5)
if ! echo "$pid_list" | grep -F "($pid)" >/dev/null; then
continue # process does not exist anymore
fi
# obtain age of pid
pid_time=$( stat -c%X "/proc/$pid" 2>/dev/null )
if [[ ! $pid_time ]]; then
continue # process does not exist anymore
fi
# kill outdated script executions (older than 12 hours / 43200 seconds)
if [[ $(date +%s --d="now - $pid_time seconds") -gt 43200 ]]; then
if kill -9 "$pid"; then
echo "Warning: Outdated script execution ($pid) has been killed!"
fi
# we are facing a race condition
else
echo "Error: Script is already running!"
exit 1
fi
done < <(pgrep -f "^/bin/bash $script_path") # obtain pids that belong to this script
mgutt
(547 rep)
Apr 3, 2023, 11:50 PM
• Last activity: Apr 18, 2023, 07:35 AM
0
votes
2
answers
394
views
How to use 'pgrep' from a script, when the checked process includes a '-c' (that 'pgrep' considers an argument)?
The following script demonstrates my problem, which is to determine if the `sox_user_auditd_v2r -c` process is running: $ cat ./pgrep_stackexchange_sample.bash #!/bin/bash -xv quoted="\'$@\'" #if pgrep -x -f $@ > /dev/null; then #if pgrep -x -f $quoted > /dev/null; then #if pgrep -f $quoted > /dev/n...
The following script demonstrates my problem, which is to determine if the
sox_user_auditd_v2r -c
process is running:
$ cat ./pgrep_stackexchange_sample.bash
#!/bin/bash -xv
quoted="\'$@\'"
#if pgrep -x -f $@ > /dev/null; then
#if pgrep -x -f $quoted > /dev/null; then
#if pgrep -f $quoted > /dev/null; then
#if pgrep -f -- $quoted > /dev/null; then
#if pgrep -x $quoted > /dev/null; then
if pgrep -x -- $quoted > /dev/null; then
echo "Process '$@' is already running."
exit 0
else
echo "Process '$@' is not running. Starting it..."
if ! "$@" &> /dev/null; then
echo "Error: Failed to start process '$@'"
exit 2
fi
echo "Process '$@' started successfully"
exit 0
fi
Namely, when I run it like so ./pgrep_stackexchange_sample.bash sox_user_auditd_v2r -c
, pgrep
would misbehave and throw an error:
+ pgrep -x -- '\'\''sox_user_auditd_v2r' '-c\'\'''
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
Namely, pgrep
considered the -c
, which should be part of the regex to be checked, as an argument for pgrep
.
--
Full run output:
$ ./pgrep_stackexchange_sample.bash sox_user_auditd_v2r -c
#!/bin/bash -xv
quoted="\'$@\'"
+ quoted='\'\''sox_user_auditd_v2r -c\'\'''
#if pgrep -x -f $@ > /dev/null; then
#if pgrep -x -f $quoted > /dev/null; then
#if pgrep -f $quoted > /dev/null; then
#if pgrep -f -- $quoted > /dev/null; then
#if pgrep -x $quoted > /dev/null; then
if pgrep -x -- $quoted > /dev/null; then
echo "Process '$@' is already running."
exit 0
else
echo "Process '$@' is not running. Starting it..."
if ! "$@" &> /dev/null; then
echo "Error: Failed to start process '$@'"
exit 2
fi
echo "Process '$@' started successfully"
exit 0
fi
+ pgrep -x -- '\'\''sox_user_auditd_v2r' '-c\'\'''
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
+ echo 'Process '\''sox_user_auditd_v2r' '-c'\'' is not running. Starting it...'
Process 'sox_user_auditd_v2r -c' is not running. Starting it...
+ sox_user_auditd_v2r -c
+ echo 'Process '\''sox_user_auditd_v2r' '-c'\'' started successfully'
Process 'sox_user_auditd_v2r -c' started successfully
+ exit 0
Can anyone suggest what would be the correct $quoted
that will allow pgrep
to work as expected?
Edit 1:
--
Trying to implement @ilkkachu answer doesn't seem to make pgrep
behave. The -c
is still considered as an argument by pgrep
:
$ ./pgrep_stackexchange_sample.bash sox_user_auditd_v2r -c
#!/bin/bash -xv
quoted="$*"
+ quoted='sox_user_auditd_v2r -c'
#if pgrep -x -f $@ > /dev/null; then
#if pgrep -x -f $quoted > /dev/null; then
#if pgrep -f $quoted > /dev/null; then
#if pgrep -f -- $quoted > /dev/null; then
#if pgrep -x $quoted > /dev/null; then
if pgrep -x -f $quoted > /dev/null; then
echo "Process '$@' is already running."
exit 0
else
echo "Process '$@' is not running. Starting it..."
if ! "$@" &> /dev/null; then
echo "Error: Failed to start process '$@'"
exit 2
fi
echo "Process '$@' started successfully"
exit 0
fi
+ pgrep -x -f sox_user_auditd_v2r -c
pgrep: invalid option -- 'c'
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
+ echo 'Process '\''sox_user_auditd_v2r' '-c'\'' is not running. Starting it...'
Process 'sox_user_auditd_v2r -c' is not running. Starting it...
+ sox_user_auditd_v2r -c
+ echo 'Process '\''sox_user_auditd_v2r' '-c'\'' started successfully'
Process 'sox_user_auditd_v2r -c' started successfully
+ exit 0
boardrider
(262 rep)
Feb 15, 2023, 06:31 PM
• Last activity: Mar 30, 2023, 09:39 PM
1
votes
1
answers
144
views
How to prevent 'pgrep' to consider the expression it checks as containing arguments for it?
I have the following script; it is supposed to start a process if it is not running: $ cat keepalive_stackexchange_sample.bash #!/bin/bash -xv # This script checks if a process is running and starts it if it's not. # If the process is already running, the script exits with exit code 0. # If the proc...
I have the following script; it is supposed to start a process if it is not running:
$ cat keepalive_stackexchange_sample.bash
#!/bin/bash -xv
# This script checks if a process is running and starts it if it's not.
# If the process is already running, the script exits with exit code 0.
# If the process was restarted by the script, the script exits with exit code 0.
# If the process fails to start, the script exits with exit code 2.
# If the first argument is "-h" or "--help", the script prints a help message and exits with exit code 10.
#
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "Usage: $0 process_name"
echo "This script checks if a process is running and starts it if it's not."
exit 10
fi
if [[ -z "$1" ]]; then
echo "Error: No process name provided."
echo "Usage: $0 process_name"
exit 12
fi
if pgrep -x -f $@ > /dev/null; then
echo "Process '$@' is already running."
exit 0
else
echo "Process '$@' is not running. Starting it..."
if ! "$@" &> /dev/null; then
echo "Error: Failed to start process '$@'"
exit 2
fi
echo "Process '$@' started successfully"
exit 0
fi
This script works fine if it the process name it gets has only one word, for instance
keepalive_stackexchange_sample.bash sox_authfiles_auditd_v2r
.
However, if the process I'm checking has arguments, pgrep
thinks these arguments are meant for it, and the script does not work as expected, for instance, if I have running:
$ ps -ef | grep [s]ox_ | grep v2r
username 12150 1 0 23:07 ? 00:00:00 sox_user_auditd_v2r -c
$
and I run keepalive_stackexchange_sample.bash sox_user_auditd_v2r -c
, I'll get the following error:
+ pgrep -x -f sox_user_auditd_v2r -c
pgrep: invalid option -- 'c'
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
+ echo 'Process '\''sox_user_auditd_v2r' '-c'\'' is not running. Starting it...'
and the script will run sox_user_auditd_v2r -c
even though it is already running.
Any suggestion how can I have the script work on processes with arguments?
boardrider
(262 rep)
Feb 14, 2023, 11:23 PM
• Last activity: Feb 19, 2023, 03:10 PM
2
votes
3
answers
392
views
Find all Processes Except These Specific Ones
Is there a `ps` or `pgrep` option or standard practice what will let me list all the running processes on a machine EXCEPT for a few specific ones I don't want to see? Put another way -- let's say I have four processes running % ps PID TTY TIME CMD 26712 ttys000 0:00.45 -zsh 28228 ttys001 0:00.23 -z...
Is there a
ps
or pgrep
option or standard practice what will let me list all the running processes on a machine EXCEPT for a few specific ones I don't want to see?
Put another way -- let's say I have four processes running
% ps
PID TTY TIME CMD
26712 ttys000 0:00.45 -zsh
28228 ttys001 0:00.23 -zsh
42083 ttys002 0:00.11 -zsh
42396 ttys002 0:00.16 node
and I have a text file with a few process IDs
% cat /tmp/no-interest.txty
26712
28228
I want a command line invocation that will display _just_ the process information for 42083 and 42396.
The more general problem I'm trying to solve is I want to show processes that started AFTER a certain point in time. I can get as far as building a list of currently running processes in a text file, but I'm blocked a bit on how to come up with a concise way to use this information to suppress the processes I don't want to see. I know how to do this with a short program in a variety of languages, but I'd like something I can invoke as a one liner and not have to worry about getting that program onto the specific machine I'm debugging.
Alana Storm
(1483 rep)
Jan 30, 2023, 11:26 PM
• Last activity: Feb 3, 2023, 08:01 AM
2
votes
1
answers
241
views
$(pgrep -f) behaviour changes within a script depending on the shebang
When I run the following script: #!/bin/bash $(pgrep -u ubuntu -f ${1} > /dev/null) echo "CURRENT_STATUS="${?} with: ./my_script.sh top when `top` is not running, it returns: CURRENT_STATUS=0 which is odd, as I would expect a `1` exit status from `pgrep`. When I remove the shebang from the script it...
When I run the following script:
#!/bin/bash
$(pgrep -u ubuntu -f ${1} > /dev/null)
echo "CURRENT_STATUS="${?}
with:
./my_script.sh top
when
top
is not running, it returns:
CURRENT_STATUS=0
which is odd, as I would expect a 1
exit status from pgrep
.
When I remove the shebang from the script it works as expected.
Could someone help me understand what's going on here?
This is on an Ubuntu 22.4.1 system.
Also note that the $(...)
doesn't change the results.
I have noticed that pgrep
seems to be matching against itself and that's why it gives a 0 result no matter what process name I feed it.
ozpac
(33 rep)
Jan 23, 2023, 05:35 PM
• Last activity: Jan 23, 2023, 07:30 PM
0
votes
1
answers
270
views
Why does pgrep not find systemd-resolved?
``` lang-bash #!/usr/bin/env bash echo "pgrep not finding systemd-resolved has bitten many times." if [ -z $(pgrep systemd-resolved) ]; then echo -e "systemd-resolved not found by pgrep, trying another way.\n"; ps aux | egrep -i '(DNS|HOST|DH|RESOLV|systemd-resolved)' | egrep -v 'grep -E'; fi; ``` `...
lang-bash
#!/usr/bin/env bash
echo "pgrep not finding systemd-resolved has bitten many times."
if [ -z $(pgrep systemd-resolved) ]; then
echo -e "systemd-resolved not found by pgrep, trying another way.\n";
ps aux | egrep -i '(DNS|HOST|DH|RESOLV|systemd-resolved)' | egrep -v 'grep -E';
fi;
systemd-resolved not found by pgrep, trying another way:
systemd+ **914** 0.0 0.0 26196 4048 ? Ss Nov12 0:02 /lib/systemd/**systemd-resolved**
rjt 73300 0.0 0.0 9228 2160 pts/2 S+ 23:02 0:00 grep -E --color=auto -i (DNS|HOST|DH|RESOLV|systemd-resolved)
I work on many different systems of various age. Need to know the backend name resolution system and what is covered by the name resolver. So I often use pgrep to find all dns related processes.
Appears to be a string length limit for pgrep?
rjt
(387 rep)
Nov 17, 2022, 05:24 AM
• Last activity: Nov 17, 2022, 08:14 AM
2
votes
2
answers
1161
views
Autokey - Focus App Window If Running, Launch App If Not
I'm trying to obtain the processID of `pcmanfm` like this: pgrep -f "pcmanfm" When `pcmanfm` is not running, the command above returns nothing (as I expect). However, when I run the command from python, it returns a process ID even when `pcmanfm` is not running: processID = os.system('pgrep -f "pcma...
I'm trying to obtain the processID of
pcmanfm
like this:
pgrep -f "pcmanfm"
When pcmanfm
is not running, the command above returns nothing (as I expect).
However, when I run the command from python, it returns a process ID even when pcmanfm
is not running:
processID = os.system('pgrep -f "pcmanfm"')
Furthermore, if you run the command above multiple times at a python3 prompt, it returns a different processID each time. All the while, pcmanfm
has been closed prior to these commands.
>>> processID = os.system('pgrep -f "pcmanfm"')
17412
>>> processID = os.system('pgrep -f "pcmanfm"')
17414
>>> processID = os.system('pgrep -f "pcmanfm"')
17416
This is really messing up my ability to launch pcmanfm
if it isn't currently running. My script thinks it is running when it isn't.
Why is this happening?
I'm actually encountering this issue in an Autokey script that I've attempted to write based on this video I watched. Here's my current script:
processID = system.exec_command('pgrep -f "pcmanfm" | head -1',True)
dialog.info_dialog("info",processID)
if (processID):
cmd = "wmctrl -lp | grep " + processID + " | awk '{print $1}'"
windowID = system.exec_command(cmd,True)
# dialog.info_dialog("info",windowID)
cmd = "wmctrl -iR " + windowID
#dialog.info_dialog("info",cmd)
system.exec_command(cmd,False)
else:
#os.system("pcmanfm /home/user/Downloads")
cmd = "/usr/bin/pcmanfm /home/user/Downloads"
system.exec_command(cmd,False)
The problem is, I keep getting processIDs even when pcmanfm
isn't running. The script properly focuses pcmanfm
if it is running, but it won't launch it if it isn't.
**Update:** I finally got this script to work by taking out -f
and replacing it with -nx
(from @they 's advice). Also, I added some exception handling to ignore autokey exceptions caused by empty output that's expected. Additionally, I converted it to a (more flexible) function so that it will service a wider variety of commands/applications:
import re
def focusOrLaunch(launchCommand):
appName = re.findall('[^\s/]+(?=\s|$)',launchCommand)
processID = None
try:
processID = system.exec_command('pgrep -nx "' + appName + '"',True)
except Exception as e:
#dialog.info_dialog("ERROR",str(e))
pass
#dialog.info_dialog("info",processID)
if (processID):
cmd = "wmctrl -lp | grep " + processID + " | awk '{print $1}'"
windowID = system.exec_command(cmd,True)
# dialog.info_dialog("info",windowID)
cmd = "wmctrl -iR " + windowID
#dialog.info_dialog("info",cmd)
system.exec_command(cmd,False)
else:
system.exec_command(launchCommand,False)
cmd = "/usr/bin/pcmanfm ~/Downloads"
focusOrLaunch(cmd)
Lonnie Best
(5415 rep)
Jan 18, 2022, 03:57 PM
• Last activity: Jan 22, 2022, 12:14 AM
0
votes
1
answers
4214
views
how to use pgrep to find a shell script process
I want to use `pgrep` command to terminate the running process in centos terminal. I tried: pgrep fedora-proxy-k8s-postgresql | xargs -r kill -9 and I could not found the process because it's a bash script running in background: ➜ pydolphin git:(tencent-cloud) ps aux|grep k8s-pos root 14131 0.0 0.0...
I want to use
pgrep
command to terminate the running process in centos terminal. I tried:
pgrep fedora-proxy-k8s-postgresql | xargs -r kill -9
and I could not found the process because it's a bash script running in background:
➜ pydolphin git:(tencent-cloud) ps aux|grep k8s-pos
root 14131 0.0 0.0 113288 1084 ? SN 01:35 0:00 bash ./fedora-proxy-k8s-postgresql.sh
What should I do to find and terminate the process using pgrep
? Or I have to use ps
command?
Dolphin
(791 rep)
Dec 27, 2021, 05:40 AM
• Last activity: Dec 27, 2021, 12:11 PM
1
votes
1
answers
1356
views
Don't understand why pgrep doesn't work in a bash script with sudo
I'm trying to detect whether the zoom program is running. I also need sudo privileges in my bash script to install it with dpkg. If I use `pgrep -f "zoom" >/dev/null 2>&1`, the return code `echo $?` will be correctly set to 0 if zoom is running and to 1 if not found. By using `sudo`, `pgrep` picked...
I'm trying to detect whether the zoom program is running. I also need sudo privileges in my bash script to install it with dpkg.
If I use
pgrep -f "zoom" >/dev/null 2>&1
, the return code echo $?
will be correctly set to 0 if zoom is running and to 1 if not found.
By using sudo
, pgrep
picked up two additional processes: the sudo pgrep etc.
one and the pgrep etc.
child process of the sudo
one. This didn't work so I had to use a 'hacky workaround': sudo pgrep -f "[z]oom" >/dev/null 2>&1
did the trick: the regex made sure pgrep
processes aren't added to the results.
So now with sudo pgrep -f "[z]oom" >/dev/null 2>&1 ;
I have echo $?
set to 1 if the process is not running and 0 if it's running. Perfect.
But when I added this in my bash script (which I run with sudo
):
if pgrep -f "[z]oom" 2>&1 ; then
echo $?
echo "zoom is running"
This no longer works and it _always_ says that the zoom process is running (so it always returns 0 even if zoom is _not_ running).
Why is that?
Dean
(484 rep)
Nov 24, 2021, 04:33 PM
• Last activity: Nov 24, 2021, 05:15 PM
1
votes
0
answers
268
views
Why pgrep hangs when clang AddressSanitizer library is preloaded using LD_PRELOAD?
I noticed that when I set LD_PRELOAD env. variable to libclang_rt.asan-x86_64.so pgrep hangs. Why is this happening and how to avoid this? senx:/$ time pgrep anything real 0m0,022s user 0m0,001s sys 0m0,020s senx:/$ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.4 senx:/$ time pgrep anything...
I noticed that when I set LD_PRELOAD env. variable to libclang_rt.asan-x86_64.so pgrep hangs.
Why is this happening and how to avoid this?
senx:/$ time pgrep anything
real 0m0,022s
user 0m0,001s
sys 0m0,020s
senx:/$ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.4
senx:/$ time pgrep anything
real 0m0,076s
user 0m0,033s
sys 0m0,027s
senx:/$ export LD_PRELOAD=/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux/libclang_rt.asan-x86_64.so
senx:/$ time pgrep anything
^C
real 0m33,077s
user 0m0,010s
sys 0m0,010s
senx
(171 rep)
Jun 1, 2021, 09:20 AM
• Last activity: Jun 2, 2021, 05:59 AM
2
votes
0
answers
449
views
Why is pgrep/pkill so much slower than ps?
I would have thought that `pgrep` would be much faster than `ps -ef` yet the contrary is true - `pgrep` takes about 4x longer. But only on my laptop e.g.: ``` $ time { ps -ef |grep foobar; } real 0m0.027s user 0m0.009s sys 0m0.023s ``` Compare with: ``` $ time pgrep foobar real 0m0.215s user 0m0.189...
I would have thought that
pgrep
would be much faster than ps -ef
yet the contrary is true - pgrep
takes about 4x longer. But only on my laptop e.g.:
$ time { ps -ef |grep foobar; }
real 0m0.027s
user 0m0.009s
sys 0m0.023s
Compare with:
$ time pgrep foobar
real 0m0.215s
user 0m0.189s
sys 0m0.021s
... the times above are representative of a typical run.
Context:
This is on an Intel i7 laptop running linux kernel 5.6.19-200.fc31.x86_64
I have another machine (a server) running 5.7.9-100.fc31.x86_64 and it does the pgrep
in about 10-12ms with roughly the same number of processes (~260).
Just weird.
I tried kernel 5.7.11 on this laptop and it made no difference - pgrep
was still about 4 times slower than ps. Same result after I rebooted to 5.6.19. Same result from the console.
On my raspberry pi-3b+ (4 cores) with raspbian, pgrep
is about twice the speed of ps!!!
My laptop has an Intel i7, 8-Gb, 4-cores with hyperthreading and about 270 processes running
The server is i5, 8-Gb, 4-core and about 265 processes running
The pi is 1-Gb, 4-core and about 133 processes running
**EDIT:** I upgraded to kernel-5.9.16-100.fc32 and now pgrep
is only about 3x slower than ps!!
**EDIT2:** a bit more info. strace on pgrep gives this:
0.000089 openat(AT_FDCWD, "/proc/29/cmdline", O_RDONLY) = 4
0.000071 read(4, "", 2047) = 0
0.000052 close(4) = 0
0.503431 stat("/proc/30", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
0.012402 openat(AT_FDCWD, "/proc/30/status", O_RDONLY) = 4
0.002080 read(4, "Name:\tmigration/4\nUmask:\t0000\nSt"..., 2048) = 967
0.000078 close(4) = 0
... so there's quite a delay in getting info on that process. There's no detail under that process so I don't know what it's about
wef
(492 rep)
Jan 6, 2021, 07:47 AM
• Last activity: Jan 6, 2021, 11:44 PM
0
votes
1
answers
277
views
IF statment for checking if arecord is working inside a shell script no responding well
I wrote a script that should check if the arecord command line audio recorder is not working, and if so it should start it. With the help of cron, I want to verify that I'm recording within a specific time frame. Thid is because sometimes the arecord stop without any warning. Here is my script (a sh...
I wrote a script that should check if the arecord command line audio recorder is not working, and if so it should start it.
With the help of cron, I want to verify that I'm recording within a specific time frame. Thid is because sometimes the arecord stop without any warning.
Here is my script (a shell executable file) I run with cron:
#!/bin/sh
dateis=$(date +"%Y-%m-%d-%H-%M")
if [pgrep -x "arecord" >/dev/null]
then
else
arecord -v -f dat -r 48 -D hw:1,0 -d 200 -c 2 | lame -r -h - record-$dateis.mp3
fi
For some reason, even when the arecord is recording, it tries to start another instance of arecord. On the command line, the pgrep is indicating properly. But for some reason it won't stop and would ignore the if statment. It starts recording even though it is running at the same moment.
Hope I made this issue clear.
Asaf Chertkoff
(111 rep)
Dec 22, 2020, 11:15 PM
• Last activity: Dec 24, 2020, 03:21 PM
1
votes
1
answers
415
views
how to write a pgrep pattern that (never) matches a zero byte?
I'd like to kill a process with a long name that ends with `foo`. Currently my plan is to use `pkill` (though for testing, to be friendly to the process, I've been checking my patterns with `pgrep` instead). I've seen [this question](https://unix.stackexchange.com/q/267007/66679) that shows that for...
I'd like to kill a process with a long name that ends with
foo
. Currently my plan is to use pkill
(though for testing, to be friendly to the process, I've been checking my patterns with pgrep
instead). I've seen [this question](https://unix.stackexchange.com/q/267007/66679) that shows that for long process names, you must use -f
to get the whole command line. The works great for getting the rest of the long process name; the problem is that then the pattern is matched against the whole command line rather than just the process name, and I would not like to accidentally kill a different program that happened to have foo
as an argument.
So I believe I would like to write a pattern something like this:
pgrep -f '^[^\0]*foo'
where I've written \0
to mean a zero byte -- the separator that -f
puts before each argument to a process. Of course \0
doesn't work; this pattern actually matches any sequence of characters that are not \
or (ASCII) 0
instead. I also tried this:
pgrep -f '^[^'echo -n '\0'
']*foo'
Here I have confirmed that my shell produces a single 0 byte for echo -n '\0'
. Unfortunately, because of the way arguments are passed, pgrep
stops scanning a pattern when it sees an actual 0 byte, so this command behaves exactly the same as pgrep -f '^[^'
would -- that is, it throws an error about receiving an invalid pattern.
How can I write a pattern that refers to a zero byte in it (or, well, any byte that isn't a zero byte)?
Daniel Wagner
(192 rep)
Sep 23, 2020, 04:36 PM
• Last activity: Nov 21, 2020, 01:56 PM
7
votes
1
answers
5354
views
How do I check with a Bash script if an application is running?
I have a gnome-run application in my home folder. I have now added the application to run when I press Meta + R (I added it in in [CCSM][1]). I run the application by executing ```./gnome-run``` in my home folder. I can't find any trace of the application process in the output of ```ps -A```. The pr...
I have a gnome-run application in my home folder. I have now added the application to run when I press Meta+R (I added it in in CCSM ). I run the application by executing
I can't find any trace of the application process in the output of
./gnome-run
in my home folder.I can't find any trace of the application process in the output of
-A
.
The problem is that if I have the gnome-run program open and I press the key combination I want the application to close. Is there a way to create a bash file that checks if the applications is running? If it is then close it, else launch it.
richie
(205 rep)
Apr 1, 2012, 05:35 PM
• Last activity: Sep 23, 2020, 08:07 PM
6
votes
2
answers
1876
views
Why doesn't "pgrep firefox" work on Ubuntu 20.04
As the title suggests, the following command on Ubuntu 20.04: pgrep firefox ... returns no results. Meanwhile, the following command: ps aux |grep firefox ... returns 6 results, 5 of which should match `pgrep firefox`. What's wrong with the original `pgrep` command on Ubuntu? ubuntu 81646 1.9 7.9 30...
As the title suggests, the following command on Ubuntu 20.04:
pgrep firefox
... returns no results.
Meanwhile, the following command:
ps aux |grep firefox
... returns 6 results, 5 of which should match
pgrep firefox
. What's wrong with the original pgrep
command on Ubuntu?
ubuntu 81646 1.9 7.9 3063696 300208 ? Sl 13:12 0:29 /usr/lib/firefox/firefox -new-window
ubuntu 81891 0.2 3.8 2473448 144796 ? Sl 13:12 0:03 /usr/lib/firefox/firefox -contentproc -childID 1 -isForBrowser -prefsLen 1 -prefMapSize 222536 -parentBuildID 20200720193547 -appdir /usr/lib/firefox/browser 81646 true tab
ubuntu 82020 0.1 3.7 2438972 141396 ? Sl 13:12 0:02 /usr/lib/firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 897 -prefMapSize 222536 -parentBuildID 20200720193547 -appdir /usr/lib/firefox/browser 81646 true tab
ubuntu 82051 0.0 2.8 2401248 107520 ? Sl 13:12 0:01 /usr/lib/firefox/firefox -contentproc -childID 4 -isForBrowser -prefsLen 6935 -prefMapSize 222536 -parentBuildID 20200720193547 -appdir /usr/lib/firefox/browser 81646 true tab
ubuntu 83658 0.0 2.0 2387704 78496 ? Sl 13:12 0:00 /usr/lib/firefox/firefox -contentproc -childID 6 -isForBrowser -prefsLen 10362 -prefMapSize 222536 -parentBuildID 20200720193547 -appdir /usr/lib/firefox/browser 81646 true tab
ubuntu 90700 0.0 0.0 17664 664 pts/0 S+ 13:37 0:00 grep --color=auto firefox
Running the same command on Ubuntu 18.04 and macOS works just fine.
tresf
(355 rep)
Aug 31, 2020, 05:40 PM
• Last activity: Sep 3, 2020, 01:18 AM
Showing page 1 of 20 total questions