Recommended way of checking only one running service?
Clash Royale CLAN TAG#URR8PPP
I am writing a shell script to check if mongod server is running and if not, to start it. To acheive that I am using the below command:
systemctl is-active --quiet mongod
This does the job as expected. But it's possible my script will run on systems that do not have systemd, so I am interested in knowing whether there is a more portable way to do this.
shell-script services portability
add a comment |
I am writing a shell script to check if mongod server is running and if not, to start it. To acheive that I am using the below command:
systemctl is-active --quiet mongod
This does the job as expected. But it's possible my script will run on systems that do not have systemd, so I am interested in knowing whether there is a more portable way to do this.
shell-script services portability
Can you assume the name of the service will be the same? I mean, will the service always be namedmongod
on all systems?
– terdon♦
Mar 1 at 12:55
@terdon Yes that we can assume.
– TheLittleNaruto
Mar 1 at 14:16
1
If the ultimate goal is to ensure the service is running, you could start it without checking — sensible service managers will only start a service if it’s not already running.
– Stephen Kitt
Mar 1 at 16:43
@Stephen Hmm that's a good point. I'll check if mongod also handle that internally.
– TheLittleNaruto
Mar 1 at 16:52
add a comment |
I am writing a shell script to check if mongod server is running and if not, to start it. To acheive that I am using the below command:
systemctl is-active --quiet mongod
This does the job as expected. But it's possible my script will run on systems that do not have systemd, so I am interested in knowing whether there is a more portable way to do this.
shell-script services portability
I am writing a shell script to check if mongod server is running and if not, to start it. To acheive that I am using the below command:
systemctl is-active --quiet mongod
This does the job as expected. But it's possible my script will run on systems that do not have systemd, so I am interested in knowing whether there is a more portable way to do this.
shell-script services portability
shell-script services portability
edited Mar 1 at 14:06
Rui F Ribeiro
41.8k1483142
41.8k1483142
asked Mar 1 at 12:19
TheLittleNarutoTheLittleNaruto
1184
1184
Can you assume the name of the service will be the same? I mean, will the service always be namedmongod
on all systems?
– terdon♦
Mar 1 at 12:55
@terdon Yes that we can assume.
– TheLittleNaruto
Mar 1 at 14:16
1
If the ultimate goal is to ensure the service is running, you could start it without checking — sensible service managers will only start a service if it’s not already running.
– Stephen Kitt
Mar 1 at 16:43
@Stephen Hmm that's a good point. I'll check if mongod also handle that internally.
– TheLittleNaruto
Mar 1 at 16:52
add a comment |
Can you assume the name of the service will be the same? I mean, will the service always be namedmongod
on all systems?
– terdon♦
Mar 1 at 12:55
@terdon Yes that we can assume.
– TheLittleNaruto
Mar 1 at 14:16
1
If the ultimate goal is to ensure the service is running, you could start it without checking — sensible service managers will only start a service if it’s not already running.
– Stephen Kitt
Mar 1 at 16:43
@Stephen Hmm that's a good point. I'll check if mongod also handle that internally.
– TheLittleNaruto
Mar 1 at 16:52
Can you assume the name of the service will be the same? I mean, will the service always be named
mongod
on all systems?– terdon♦
Mar 1 at 12:55
Can you assume the name of the service will be the same? I mean, will the service always be named
mongod
on all systems?– terdon♦
Mar 1 at 12:55
@terdon Yes that we can assume.
– TheLittleNaruto
Mar 1 at 14:16
@terdon Yes that we can assume.
– TheLittleNaruto
Mar 1 at 14:16
1
1
If the ultimate goal is to ensure the service is running, you could start it without checking — sensible service managers will only start a service if it’s not already running.
– Stephen Kitt
Mar 1 at 16:43
If the ultimate goal is to ensure the service is running, you could start it without checking — sensible service managers will only start a service if it’s not already running.
– Stephen Kitt
Mar 1 at 16:43
@Stephen Hmm that's a good point. I'll check if mongod also handle that internally.
– TheLittleNaruto
Mar 1 at 16:52
@Stephen Hmm that's a good point. I'll check if mongod also handle that internally.
– TheLittleNaruto
Mar 1 at 16:52
add a comment |
2 Answers
2
active
oldest
votes
This is not perfect, but will work on many systems. Note that the pgrep
approach is suboptimal since the name might be slightly different, or you might pass a name that is a substring of another name, but it's a start.
#!/usr/bin/sh
if systemctl is-active --quiet "$1" > /dev/null 2>&1; then
echo "Running! (systemctl)"
elif service "$1" status 2>&1 | grep -Eq 'is running|: active (running)'; then
echo "Running! (service)"
elif pgrep "$1" > /dev/null 2>&1 ; then
echo "Running (pgrep)"
else
echo "Not running"
fi
Save that as checkIfRunning
and run it like this:
checkIfRunning mongod
Oh this looks clean approach. But still we are parsing the output right ? Do you think it's not a problem ?
– TheLittleNaruto
Mar 1 at 15:31
@TheLittleNaruto no, I think it is absolutely a problem :) But I can't think of any way to avoid that if you need to go for portability.
– terdon♦
Mar 1 at 16:47
Hmm thought same. :) What do you think about the way I mentioned in my question ? Is it better than your approach ?
– TheLittleNaruto
Mar 1 at 18:27
@TheLittleNaruto what do you mean? I do the exact same thing as you have in your question here. The others are only executed if the first one fails.
– terdon♦
Mar 1 at 18:29
1
@Zanna d'oh! Of course we shouldn't redirect both streams. Total PEBKAC there, thanks. I got carried away by the others. And good point about the non-systemdservice
output, thanks!
– terdon♦
Mar 2 at 14:06
add a comment |
There is no one, portable way of achieving that. But many current Linux distributions have old, legacy service
command that behaves as a proxy to systemctl
if run on systemd powered OS.
So you can run the command like this:
# service sshd status
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-02-20 12:16:17 UTC; 1 weeks 2 days ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 3388 (sshd)
CGroup: /system.slice/sshd.service
└─3388 /usr/sbin/sshd -D
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%2f503769%2frecommended-way-of-checking-only-one-running-service%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
This is not perfect, but will work on many systems. Note that the pgrep
approach is suboptimal since the name might be slightly different, or you might pass a name that is a substring of another name, but it's a start.
#!/usr/bin/sh
if systemctl is-active --quiet "$1" > /dev/null 2>&1; then
echo "Running! (systemctl)"
elif service "$1" status 2>&1 | grep -Eq 'is running|: active (running)'; then
echo "Running! (service)"
elif pgrep "$1" > /dev/null 2>&1 ; then
echo "Running (pgrep)"
else
echo "Not running"
fi
Save that as checkIfRunning
and run it like this:
checkIfRunning mongod
Oh this looks clean approach. But still we are parsing the output right ? Do you think it's not a problem ?
– TheLittleNaruto
Mar 1 at 15:31
@TheLittleNaruto no, I think it is absolutely a problem :) But I can't think of any way to avoid that if you need to go for portability.
– terdon♦
Mar 1 at 16:47
Hmm thought same. :) What do you think about the way I mentioned in my question ? Is it better than your approach ?
– TheLittleNaruto
Mar 1 at 18:27
@TheLittleNaruto what do you mean? I do the exact same thing as you have in your question here. The others are only executed if the first one fails.
– terdon♦
Mar 1 at 18:29
1
@Zanna d'oh! Of course we shouldn't redirect both streams. Total PEBKAC there, thanks. I got carried away by the others. And good point about the non-systemdservice
output, thanks!
– terdon♦
Mar 2 at 14:06
add a comment |
This is not perfect, but will work on many systems. Note that the pgrep
approach is suboptimal since the name might be slightly different, or you might pass a name that is a substring of another name, but it's a start.
#!/usr/bin/sh
if systemctl is-active --quiet "$1" > /dev/null 2>&1; then
echo "Running! (systemctl)"
elif service "$1" status 2>&1 | grep -Eq 'is running|: active (running)'; then
echo "Running! (service)"
elif pgrep "$1" > /dev/null 2>&1 ; then
echo "Running (pgrep)"
else
echo "Not running"
fi
Save that as checkIfRunning
and run it like this:
checkIfRunning mongod
Oh this looks clean approach. But still we are parsing the output right ? Do you think it's not a problem ?
– TheLittleNaruto
Mar 1 at 15:31
@TheLittleNaruto no, I think it is absolutely a problem :) But I can't think of any way to avoid that if you need to go for portability.
– terdon♦
Mar 1 at 16:47
Hmm thought same. :) What do you think about the way I mentioned in my question ? Is it better than your approach ?
– TheLittleNaruto
Mar 1 at 18:27
@TheLittleNaruto what do you mean? I do the exact same thing as you have in your question here. The others are only executed if the first one fails.
– terdon♦
Mar 1 at 18:29
1
@Zanna d'oh! Of course we shouldn't redirect both streams. Total PEBKAC there, thanks. I got carried away by the others. And good point about the non-systemdservice
output, thanks!
– terdon♦
Mar 2 at 14:06
add a comment |
This is not perfect, but will work on many systems. Note that the pgrep
approach is suboptimal since the name might be slightly different, or you might pass a name that is a substring of another name, but it's a start.
#!/usr/bin/sh
if systemctl is-active --quiet "$1" > /dev/null 2>&1; then
echo "Running! (systemctl)"
elif service "$1" status 2>&1 | grep -Eq 'is running|: active (running)'; then
echo "Running! (service)"
elif pgrep "$1" > /dev/null 2>&1 ; then
echo "Running (pgrep)"
else
echo "Not running"
fi
Save that as checkIfRunning
and run it like this:
checkIfRunning mongod
This is not perfect, but will work on many systems. Note that the pgrep
approach is suboptimal since the name might be slightly different, or you might pass a name that is a substring of another name, but it's a start.
#!/usr/bin/sh
if systemctl is-active --quiet "$1" > /dev/null 2>&1; then
echo "Running! (systemctl)"
elif service "$1" status 2>&1 | grep -Eq 'is running|: active (running)'; then
echo "Running! (service)"
elif pgrep "$1" > /dev/null 2>&1 ; then
echo "Running (pgrep)"
else
echo "Not running"
fi
Save that as checkIfRunning
and run it like this:
checkIfRunning mongod
edited Mar 2 at 14:05
answered Mar 1 at 14:25
terdon♦terdon
133k32266446
133k32266446
Oh this looks clean approach. But still we are parsing the output right ? Do you think it's not a problem ?
– TheLittleNaruto
Mar 1 at 15:31
@TheLittleNaruto no, I think it is absolutely a problem :) But I can't think of any way to avoid that if you need to go for portability.
– terdon♦
Mar 1 at 16:47
Hmm thought same. :) What do you think about the way I mentioned in my question ? Is it better than your approach ?
– TheLittleNaruto
Mar 1 at 18:27
@TheLittleNaruto what do you mean? I do the exact same thing as you have in your question here. The others are only executed if the first one fails.
– terdon♦
Mar 1 at 18:29
1
@Zanna d'oh! Of course we shouldn't redirect both streams. Total PEBKAC there, thanks. I got carried away by the others. And good point about the non-systemdservice
output, thanks!
– terdon♦
Mar 2 at 14:06
add a comment |
Oh this looks clean approach. But still we are parsing the output right ? Do you think it's not a problem ?
– TheLittleNaruto
Mar 1 at 15:31
@TheLittleNaruto no, I think it is absolutely a problem :) But I can't think of any way to avoid that if you need to go for portability.
– terdon♦
Mar 1 at 16:47
Hmm thought same. :) What do you think about the way I mentioned in my question ? Is it better than your approach ?
– TheLittleNaruto
Mar 1 at 18:27
@TheLittleNaruto what do you mean? I do the exact same thing as you have in your question here. The others are only executed if the first one fails.
– terdon♦
Mar 1 at 18:29
1
@Zanna d'oh! Of course we shouldn't redirect both streams. Total PEBKAC there, thanks. I got carried away by the others. And good point about the non-systemdservice
output, thanks!
– terdon♦
Mar 2 at 14:06
Oh this looks clean approach. But still we are parsing the output right ? Do you think it's not a problem ?
– TheLittleNaruto
Mar 1 at 15:31
Oh this looks clean approach. But still we are parsing the output right ? Do you think it's not a problem ?
– TheLittleNaruto
Mar 1 at 15:31
@TheLittleNaruto no, I think it is absolutely a problem :) But I can't think of any way to avoid that if you need to go for portability.
– terdon♦
Mar 1 at 16:47
@TheLittleNaruto no, I think it is absolutely a problem :) But I can't think of any way to avoid that if you need to go for portability.
– terdon♦
Mar 1 at 16:47
Hmm thought same. :) What do you think about the way I mentioned in my question ? Is it better than your approach ?
– TheLittleNaruto
Mar 1 at 18:27
Hmm thought same. :) What do you think about the way I mentioned in my question ? Is it better than your approach ?
– TheLittleNaruto
Mar 1 at 18:27
@TheLittleNaruto what do you mean? I do the exact same thing as you have in your question here. The others are only executed if the first one fails.
– terdon♦
Mar 1 at 18:29
@TheLittleNaruto what do you mean? I do the exact same thing as you have in your question here. The others are only executed if the first one fails.
– terdon♦
Mar 1 at 18:29
1
1
@Zanna d'oh! Of course we shouldn't redirect both streams. Total PEBKAC there, thanks. I got carried away by the others. And good point about the non-systemd
service
output, thanks!– terdon♦
Mar 2 at 14:06
@Zanna d'oh! Of course we shouldn't redirect both streams. Total PEBKAC there, thanks. I got carried away by the others. And good point about the non-systemd
service
output, thanks!– terdon♦
Mar 2 at 14:06
add a comment |
There is no one, portable way of achieving that. But many current Linux distributions have old, legacy service
command that behaves as a proxy to systemctl
if run on systemd powered OS.
So you can run the command like this:
# service sshd status
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-02-20 12:16:17 UTC; 1 weeks 2 days ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 3388 (sshd)
CGroup: /system.slice/sshd.service
└─3388 /usr/sbin/sshd -D
add a comment |
There is no one, portable way of achieving that. But many current Linux distributions have old, legacy service
command that behaves as a proxy to systemctl
if run on systemd powered OS.
So you can run the command like this:
# service sshd status
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-02-20 12:16:17 UTC; 1 weeks 2 days ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 3388 (sshd)
CGroup: /system.slice/sshd.service
└─3388 /usr/sbin/sshd -D
add a comment |
There is no one, portable way of achieving that. But many current Linux distributions have old, legacy service
command that behaves as a proxy to systemctl
if run on systemd powered OS.
So you can run the command like this:
# service sshd status
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-02-20 12:16:17 UTC; 1 weeks 2 days ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 3388 (sshd)
CGroup: /system.slice/sshd.service
└─3388 /usr/sbin/sshd -D
There is no one, portable way of achieving that. But many current Linux distributions have old, legacy service
command that behaves as a proxy to systemctl
if run on systemd powered OS.
So you can run the command like this:
# service sshd status
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-02-20 12:16:17 UTC; 1 weeks 2 days ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 3388 (sshd)
CGroup: /system.slice/sshd.service
└─3388 /usr/sbin/sshd -D
answered Mar 1 at 14:10
SzczadSzczad
1615
1615
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.
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%2f503769%2frecommended-way-of-checking-only-one-running-service%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
Can you assume the name of the service will be the same? I mean, will the service always be named
mongod
on all systems?– terdon♦
Mar 1 at 12:55
@terdon Yes that we can assume.
– TheLittleNaruto
Mar 1 at 14:16
1
If the ultimate goal is to ensure the service is running, you could start it without checking — sensible service managers will only start a service if it’s not already running.
– Stephen Kitt
Mar 1 at 16:43
@Stephen Hmm that's a good point. I'll check if mongod also handle that internally.
– TheLittleNaruto
Mar 1 at 16:52