Condition sometimes failing to test if a specific process is running or not
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
shell-script process livecd xubuntu
add a comment |
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
shell-script process livecd xubuntu
add a comment |
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
shell-script process livecd xubuntu
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
shell-script process livecd xubuntu
shell-script process livecd xubuntu
edited Dec 18 '18 at 23:15
asked Dec 18 '18 at 19:25
Lucas Rizzini
267
267
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
- Ps has
-C
option without need to grep
killall -0 firefox-bin
will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.- Just remove your
if ps
and leavekillall -9 firefox-bin && firefox-bin &> /dev/null &
. So if process will not be killed, it wouldn't be started.
add a comment |
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.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489754%2fcondition-sometimes-failing-to-test-if-a-specific-process-is-running-or-not%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
- Ps has
-C
option without need to grep
killall -0 firefox-bin
will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.- Just remove your
if ps
and leavekillall -9 firefox-bin && firefox-bin &> /dev/null &
. So if process will not be killed, it wouldn't be started.
add a comment |
- Ps has
-C
option without need to grep
killall -0 firefox-bin
will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.- Just remove your
if ps
and leavekillall -9 firefox-bin && firefox-bin &> /dev/null &
. So if process will not be killed, it wouldn't be started.
add a comment |
- Ps has
-C
option without need to grep
killall -0 firefox-bin
will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.- Just remove your
if ps
and leavekillall -9 firefox-bin && firefox-bin &> /dev/null &
. So if process will not be killed, it wouldn't be started.
- Ps has
-C
option without need to grep
killall -0 firefox-bin
will tell you if firefox-bin process exist by exit code. Without need of pipe and redirection in a raw.- Just remove your
if ps
and leavekillall -9 firefox-bin && firefox-bin &> /dev/null &
. So if process will not be killed, it wouldn't be started.
answered Dec 19 '18 at 1:25
Alexander
1,151213
1,151213
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 18 '18 at 23:35
Doug O'Neal
2,8321817
2,8321817
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489754%2fcondition-sometimes-failing-to-test-if-a-specific-process-is-running-or-not%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown