Bash script to verify if a process is running is not working
I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:
#!/bin/bash
# Script to check if fail2ban service is running
if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi
I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status
command.
But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux
command too instead of pgrep
. But, I still get the same result.
bash process administrator
New contributor
|
show 2 more comments
I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:
#!/bin/bash
# Script to check if fail2ban service is running
if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi
I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status
command.
But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux
command too instead of pgrep
. But, I still get the same result.
bash process administrator
New contributor
4
Why even try to cobble something together withps
, when you can usesystemctl is-active fail2ban
? FWIW your command probably fails because you specified an exact match (-x
) but the process name is actuallyfail2ban.server
– steeldriver
14 hours ago
Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?
– skr
14 hours ago
Oh that is right. Thank you so much.
– skr
14 hours ago
@skr Re: "... Is ... in all redhat systems?": Redhat is off-topic on AskUbuntu.
– PerlDuck
13 hours ago
@steeldriver That's the service name, not the process name.
– Lightness Races in Orbit
12 hours ago
|
show 2 more comments
I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:
#!/bin/bash
# Script to check if fail2ban service is running
if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi
I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status
command.
But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux
command too instead of pgrep
. But, I still get the same result.
bash process administrator
New contributor
I have a simple script as below which checks if fail2ban service is running or not on Ubuntu 18.04:
#!/bin/bash
# Script to check if fail2ban service is running
if pgrep -x "fail2ban" > /dev/null
then
echo "Fail2ban is running"
else
echo "Fail2ban is not running"
fi
I have installed fail2ban in a test VM and is running on the VM. Here is a screenshot of systemctl status
command.
But, when the run the above script, I get the result that "Fail2ban is not running". I am not sure if is with the script. I tried ps aux
command too instead of pgrep
. But, I still get the same result.
bash process administrator
bash process administrator
New contributor
New contributor
edited 10 hours ago
Pablo Bianchi
2,4451530
2,4451530
New contributor
asked 14 hours ago
skrskr
263
263
New contributor
New contributor
4
Why even try to cobble something together withps
, when you can usesystemctl is-active fail2ban
? FWIW your command probably fails because you specified an exact match (-x
) but the process name is actuallyfail2ban.server
– steeldriver
14 hours ago
Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?
– skr
14 hours ago
Oh that is right. Thank you so much.
– skr
14 hours ago
@skr Re: "... Is ... in all redhat systems?": Redhat is off-topic on AskUbuntu.
– PerlDuck
13 hours ago
@steeldriver That's the service name, not the process name.
– Lightness Races in Orbit
12 hours ago
|
show 2 more comments
4
Why even try to cobble something together withps
, when you can usesystemctl is-active fail2ban
? FWIW your command probably fails because you specified an exact match (-x
) but the process name is actuallyfail2ban.server
– steeldriver
14 hours ago
Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?
– skr
14 hours ago
Oh that is right. Thank you so much.
– skr
14 hours ago
@skr Re: "... Is ... in all redhat systems?": Redhat is off-topic on AskUbuntu.
– PerlDuck
13 hours ago
@steeldriver That's the service name, not the process name.
– Lightness Races in Orbit
12 hours ago
4
4
Why even try to cobble something together with
ps
, when you can use systemctl is-active fail2ban
? FWIW your command probably fails because you specified an exact match (-x
) but the process name is actually fail2ban.server
– steeldriver
14 hours ago
Why even try to cobble something together with
ps
, when you can use systemctl is-active fail2ban
? FWIW your command probably fails because you specified an exact match (-x
) but the process name is actually fail2ban.server
– steeldriver
14 hours ago
Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?
– skr
14 hours ago
Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?
– skr
14 hours ago
Oh that is right. Thank you so much.
– skr
14 hours ago
Oh that is right. Thank you so much.
– skr
14 hours ago
@skr Re: "... Is ... in all redhat systems?": Redhat is off-topic on AskUbuntu.
– PerlDuck
13 hours ago
@skr Re: "... Is ... in all redhat systems?": Redhat is off-topic on AskUbuntu.
– PerlDuck
13 hours ago
@steeldriver That's the service name, not the process name.
– Lightness Races in Orbit
12 hours ago
@steeldriver That's the service name, not the process name.
– Lightness Races in Orbit
12 hours ago
|
show 2 more comments
1 Answer
1
active
oldest
votes
You asked pgrep
to exactly (-x
) search for a process called fail2ban
but the output of systemctl status
shows it is called
/usr/bin/python3
instead.
To check whether a systemd
unit is running use
systemctl is-active --quiet fail2ban
That is:
if systemctl is-active --quiet fail2ban; then
echo "running"
else
echo "not running"
fi
2
Alternatively, one could usepgrep -f '*fail2ban*'
to pattern-match orpgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server'
to match exactly what screenshots reveal
– Sergiy Kolodyazhnyy
10 hours ago
1
pgrep -f '*fail2ban*'
could catch thepgrep
command itself.
– Dev
10 hours ago
At least on my 16.04 system, in spite of it being a python script, the process name (as seen in/proc/<pid>/comm
) isfail2ban-server
sopgrep -x fail2ban-server
does work - although I see no reason to use it whensystemctl is-active
is available
– steeldriver
7 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
skr is a new contributor. Be nice, and check out our Code of Conduct.
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%2faskubuntu.com%2fquestions%2f1110915%2fbash-script-to-verify-if-a-process-is-running-is-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You asked pgrep
to exactly (-x
) search for a process called fail2ban
but the output of systemctl status
shows it is called
/usr/bin/python3
instead.
To check whether a systemd
unit is running use
systemctl is-active --quiet fail2ban
That is:
if systemctl is-active --quiet fail2ban; then
echo "running"
else
echo "not running"
fi
2
Alternatively, one could usepgrep -f '*fail2ban*'
to pattern-match orpgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server'
to match exactly what screenshots reveal
– Sergiy Kolodyazhnyy
10 hours ago
1
pgrep -f '*fail2ban*'
could catch thepgrep
command itself.
– Dev
10 hours ago
At least on my 16.04 system, in spite of it being a python script, the process name (as seen in/proc/<pid>/comm
) isfail2ban-server
sopgrep -x fail2ban-server
does work - although I see no reason to use it whensystemctl is-active
is available
– steeldriver
7 hours ago
add a comment |
You asked pgrep
to exactly (-x
) search for a process called fail2ban
but the output of systemctl status
shows it is called
/usr/bin/python3
instead.
To check whether a systemd
unit is running use
systemctl is-active --quiet fail2ban
That is:
if systemctl is-active --quiet fail2ban; then
echo "running"
else
echo "not running"
fi
2
Alternatively, one could usepgrep -f '*fail2ban*'
to pattern-match orpgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server'
to match exactly what screenshots reveal
– Sergiy Kolodyazhnyy
10 hours ago
1
pgrep -f '*fail2ban*'
could catch thepgrep
command itself.
– Dev
10 hours ago
At least on my 16.04 system, in spite of it being a python script, the process name (as seen in/proc/<pid>/comm
) isfail2ban-server
sopgrep -x fail2ban-server
does work - although I see no reason to use it whensystemctl is-active
is available
– steeldriver
7 hours ago
add a comment |
You asked pgrep
to exactly (-x
) search for a process called fail2ban
but the output of systemctl status
shows it is called
/usr/bin/python3
instead.
To check whether a systemd
unit is running use
systemctl is-active --quiet fail2ban
That is:
if systemctl is-active --quiet fail2ban; then
echo "running"
else
echo "not running"
fi
You asked pgrep
to exactly (-x
) search for a process called fail2ban
but the output of systemctl status
shows it is called
/usr/bin/python3
instead.
To check whether a systemd
unit is running use
systemctl is-active --quiet fail2ban
That is:
if systemctl is-active --quiet fail2ban; then
echo "running"
else
echo "not running"
fi
answered 14 hours ago
PerlDuckPerlDuck
5,85111333
5,85111333
2
Alternatively, one could usepgrep -f '*fail2ban*'
to pattern-match orpgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server'
to match exactly what screenshots reveal
– Sergiy Kolodyazhnyy
10 hours ago
1
pgrep -f '*fail2ban*'
could catch thepgrep
command itself.
– Dev
10 hours ago
At least on my 16.04 system, in spite of it being a python script, the process name (as seen in/proc/<pid>/comm
) isfail2ban-server
sopgrep -x fail2ban-server
does work - although I see no reason to use it whensystemctl is-active
is available
– steeldriver
7 hours ago
add a comment |
2
Alternatively, one could usepgrep -f '*fail2ban*'
to pattern-match orpgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server'
to match exactly what screenshots reveal
– Sergiy Kolodyazhnyy
10 hours ago
1
pgrep -f '*fail2ban*'
could catch thepgrep
command itself.
– Dev
10 hours ago
At least on my 16.04 system, in spite of it being a python script, the process name (as seen in/proc/<pid>/comm
) isfail2ban-server
sopgrep -x fail2ban-server
does work - although I see no reason to use it whensystemctl is-active
is available
– steeldriver
7 hours ago
2
2
Alternatively, one could use
pgrep -f '*fail2ban*'
to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server'
to match exactly what screenshots reveal– Sergiy Kolodyazhnyy
10 hours ago
Alternatively, one could use
pgrep -f '*fail2ban*'
to pattern-match or pgrep -f '/usr/bin/python3 /usr/bin/fail2ban-server'
to match exactly what screenshots reveal– Sergiy Kolodyazhnyy
10 hours ago
1
1
pgrep -f '*fail2ban*'
could catch the pgrep
command itself.– Dev
10 hours ago
pgrep -f '*fail2ban*'
could catch the pgrep
command itself.– Dev
10 hours ago
At least on my 16.04 system, in spite of it being a python script, the process name (as seen in
/proc/<pid>/comm
) is fail2ban-server
so pgrep -x fail2ban-server
does work - although I see no reason to use it when systemctl is-active
is available– steeldriver
7 hours ago
At least on my 16.04 system, in spite of it being a python script, the process name (as seen in
/proc/<pid>/comm
) is fail2ban-server
so pgrep -x fail2ban-server
does work - although I see no reason to use it when systemctl is-active
is available– steeldriver
7 hours ago
add a comment |
skr is a new contributor. Be nice, and check out our Code of Conduct.
skr is a new contributor. Be nice, and check out our Code of Conduct.
skr is a new contributor. Be nice, and check out our Code of Conduct.
skr is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1110915%2fbash-script-to-verify-if-a-process-is-running-is-not-working%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
4
Why even try to cobble something together with
ps
, when you can usesystemctl is-active fail2ban
? FWIW your command probably fails because you specified an exact match (-x
) but the process name is actuallyfail2ban.server
– steeldriver
14 hours ago
Ok. Thanks. It worked, I changed the command to the above command. But, is there any way you know why pgrep will not work? Also, Is systemctl command available by default in all redhat systems?
– skr
14 hours ago
Oh that is right. Thank you so much.
– skr
14 hours ago
@skr Re: "... Is ... in all redhat systems?": Redhat is off-topic on AskUbuntu.
– PerlDuck
13 hours ago
@steeldriver That's the service name, not the process name.
– Lightness Races in Orbit
12 hours ago