Skip to content

Improve process running detection method #20

@academo

Description

@academo

From @asensi comment #14 (comment)

@asensi yes that makes sense, it was the original way I was doing it but I started getting issues and that's why I removed that part of the code.

I had a very strange issue where the script works perfectly if invoked from the terminal but acts stranged when invoked via a kde shortcut. it is like somehow the pgrep doesn't report the same processes.

I am thinking of replacing pgrep all together with good old ps -A and use the grep -v as I had it before. wdyt?

@academo: It may work. For the shortcut problem, can you try replacing

USER_FILTER=""
if [[ -n "$CURRENTUSERONLY" ]] && command -v loginctl >/dev/null 2>&1; then
	if command -v loginctl >/dev/null 2>&1; then
   	session_id=$(loginctl show-seat seat0 -p ActiveSession --value)
   	user_id=$(loginctl show-session "$session_id" -p User --value)
   	USER_FILTER="-u $user_id"
	fi
fi

# Note: In this case, `$USER_FILTER` must not have quotes around it.
# shellcheck disable=SC2086
IS_RUNNING=$(pgrep $USER_FILTER -o -a -f "$PROCESS" --ignore-ancestors)

with

# TESTING...
# USER_FILTER=""
# if [[ -n "$CURRENTUSERONLY" ]] && command -v loginctl >/dev/null 2>&1; then
# 	if command -v loginctl >/dev/null 2>&1; then
#    	session_id=$(loginctl show-seat seat0 -p ActiveSession --value)
#    	user_id=$(loginctl show-session "$session_id" -p User --value)
#    	USER_FILTER="-u $user_id"
# 	fi
# fi

# TESTING...
IS_RUNNING="$(ps -C "$PROCESS" -o user,pid,comm --no-headers --sort=start_time)"   # [^no_final_exit_1]
# [^no_final_exit_1]: A final `|| exit 1` must not be used because what is searched may exist or not

if [[ -n "$CURRENTUSERONLY" ]]; then
    # Note: The `-u` and `-C` parameters of `ps` have a particular way of being used. As it was written on 
    # <https://serverfault.com/questions/197647/can-the-linux-ps-command-output-be-filtered-by-user-and-command>: 
    # "I can filter a process per user with "ps -u xxx" and its command by "ps -C yyy", but when I try "ps -u 
    # xxx -C yyy", they are combined using OR logic. I need AND logic"
    IS_RUNNING="$(grep --max-count=1 "^$USER " <<< "$IS_RUNNING")"   # [^no_final_exit_1]
else
    IS_RUNNING="$(head -1 <<< "$IS_RUNNING")" || exit 1
fi

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions