improvement suggestion.

Post Reply
gathlete
Posts: 8
Joined: Sat Jan 06, 2024 8:15 am
Been thanked: 1 time

Code: Select all

vesta/func/remote.sh
there is a potential issue in the is_procces_running function. The function checks if the current script is already running by comparing the process IDs (PIDs) of all running instances of the script. If a matching PID is found, the function returns an error message indicating that the script is already running.
The problem with this approach is that the function uses the pidof command to retrieve the PIDs of all running instances of the script. However, the pidof command may return the PID of the current instance of the script as well. This means that the function will always detect itself as a running instance of the script, even if it is the only instance running.
The cause of the issue is the use of the pidof command in the is_procces_running function. The pidof command returns the PIDs of all running instances of a given process, including the current instance. This causes the function to always detect itself as a running instance of the script.
To fix the issue, we need to modify the is_procces_running function to exclude the current instance of the script from the list of running instances. We can do this by filtering out the PID of the current instance from the list of PIDs returned by the pidof command.
Here's the modified code for the is_procces_running function:

Code: Select all

language-sh
is_procces_running() {
    SCRIPT=$(basename $0)
    CURRENT_PID=$$
    for pid in $(pidof -x $SCRIPT); do
        if [ $pid != $CURRENT_PID ]; then
            check_result $E_INUSE "$SCRIPT is already running"
        fi
    done
}
In the modified code, we store the PID of the current instance of the script in the CURRENT_PID variable. Then, we compare each PID returned by the pidof command with the CURRENT_PID variable. If a matching PID is found, we skip it and continue to the next PID. This ensures that the function does not detect the current instance of the script as a running instance.
Post Reply