0

I can't understand why my script, sometimes, won't recognize if my Firefox is running. Analyzing the stdout I could state that this condition (if ps cax | grep firefox > /dev/null) sometimes is skipped even when my Firefox is running.

Currently using Xubuntu 18.10 Livecd

The script called at startup.

#!/bin/bash 

[[ -f /home/xubuntu/controle_memoria.lock ]] && exit 1
> /home/xubuntu/controle_memoria.lock

while true ; do 
free=`free -m | grep Mem | awk '{print $4}'`
if [ "$free" -gt 0 ]
 then
    if [ $free -le 120 ]; #When my memory consuptiom goes below 120MB do the commands below. 
     then

if ps cax | grep firefox > /dev/null
then
        sudo killall -9 firefox-bin         
        firefox &> /dev/null &

else
    echo "Stopped"
fi

        sudo sysctl -w vm.drop_caches=3
        sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches 



    fi
fi & sleep 1; done
1
  1. Ps has -C option without need to grep
  2. killall -0 firefox-bin will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.
  3. Just remove your if ps and leave killall -9 firefox-bin && firefox-bin &> /dev/null &. So if process will not be killed, it wouldn't be started.
0

What happens if you replace your if ps block with

for proc in /proc/*
do
    if [[ $(readlink -f ${proc}/exe) = "/usr/bin/firefox-bin" ]]
        sudo killall -9 firefox-bin
        firefox &> /dev/null &
    else
        echo "Stopped"
    fi
done

This removes trying to parse ps output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.