Cronjob run program only if other programm is already running












1















how do i run a cronjob only when another service is already up running and while the programm itself is not already running? It is important that programm2 starts only after programm1 is already up.



*/2 * * * * check if programm1 one is running ; check if programm2 is not running ; /etc/init.d programm2 start










share|improve this question


















  • 4





    Services that depend on other services... sounds like a task for systemd.

    – danzel
    Feb 3 at 12:41
















1















how do i run a cronjob only when another service is already up running and while the programm itself is not already running? It is important that programm2 starts only after programm1 is already up.



*/2 * * * * check if programm1 one is running ; check if programm2 is not running ; /etc/init.d programm2 start










share|improve this question


















  • 4





    Services that depend on other services... sounds like a task for systemd.

    – danzel
    Feb 3 at 12:41














1












1








1








how do i run a cronjob only when another service is already up running and while the programm itself is not already running? It is important that programm2 starts only after programm1 is already up.



*/2 * * * * check if programm1 one is running ; check if programm2 is not running ; /etc/init.d programm2 start










share|improve this question














how do i run a cronjob only when another service is already up running and while the programm itself is not already running? It is important that programm2 starts only after programm1 is already up.



*/2 * * * * check if programm1 one is running ; check if programm2 is not running ; /etc/init.d programm2 start







cron






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 3 at 12:16









La FleurLa Fleur

61




61








  • 4





    Services that depend on other services... sounds like a task for systemd.

    – danzel
    Feb 3 at 12:41














  • 4





    Services that depend on other services... sounds like a task for systemd.

    – danzel
    Feb 3 at 12:41








4




4





Services that depend on other services... sounds like a task for systemd.

– danzel
Feb 3 at 12:41





Services that depend on other services... sounds like a task for systemd.

– danzel
Feb 3 at 12:41










1 Answer
1






active

oldest

votes


















1














You could write a script using the $? variable. This variable contains the exit status of the last program; so if you run systemctl status dnsmasq and it's active, running echo $? will return 0 which means true. If it's not active, $? will return a non-0 answer, which means false.



A simple script to achieve this function would be:



#/!bin/bash
x=1
while [ "$x" != "0" ]
do
systemctl status (service to monitor)
if [ "$?" = "0" ]
then
systemctl start (service to start)
break
else
sleep 30
continue
fi
done


Line 1 = sets a variable to non-zero so a loop can be run from it



Line 2 = starts a loop that runs continuously (x can never = 0)



Line 4 = checks if a service is running, which sets $? to a zero or non-zero
value



Line 5 onward = checks if the exit status of the last command (checking if the service is running) was true or false. if it's true (zero), it starts the next service and breaks out of the loop. If it's false (non-zero) it returns to the start of the loop and runs until the selected service is running.



You could either run this script at startup or turn it into a service of its own.



You would need to run it as root, whichever your choice, since you're starting/stopping system services.






share|improve this answer





















  • 1





    You are re-implementing the systemd attributes Requires= and/or BindsTo= and/or After=. Also, the while loop will probably make the CPU fans blow because it re-runs systemctl without any pause.

    – PerlDuck
    Feb 3 at 16:00








  • 1





    @perlduck added a 30 second sleep in the else statement to address the inefficiency of running the script continuously. In regards to the former, the asker wanted a cronjob so I figured a simple script would be the most flexible solution, though not the most efficient. Thanks for the pointers.

    – Minty
    Feb 4 at 0:23











  • @Minty if this script is called by the cronjob mentioned by the asker, wouldn't this queue a new instance of your script every 5 minutes if the service it is waiting for is not running? If you're already using systemd, you might as well use systemd timer instead of cron and the mechanisms mentioned by PerlDuck for a pure systemd solution.

    – danzel
    Feb 4 at 10:27












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1115237%2fcronjob-run-program-only-if-other-programm-is-already-running%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









1














You could write a script using the $? variable. This variable contains the exit status of the last program; so if you run systemctl status dnsmasq and it's active, running echo $? will return 0 which means true. If it's not active, $? will return a non-0 answer, which means false.



A simple script to achieve this function would be:



#/!bin/bash
x=1
while [ "$x" != "0" ]
do
systemctl status (service to monitor)
if [ "$?" = "0" ]
then
systemctl start (service to start)
break
else
sleep 30
continue
fi
done


Line 1 = sets a variable to non-zero so a loop can be run from it



Line 2 = starts a loop that runs continuously (x can never = 0)



Line 4 = checks if a service is running, which sets $? to a zero or non-zero
value



Line 5 onward = checks if the exit status of the last command (checking if the service is running) was true or false. if it's true (zero), it starts the next service and breaks out of the loop. If it's false (non-zero) it returns to the start of the loop and runs until the selected service is running.



You could either run this script at startup or turn it into a service of its own.



You would need to run it as root, whichever your choice, since you're starting/stopping system services.






share|improve this answer





















  • 1





    You are re-implementing the systemd attributes Requires= and/or BindsTo= and/or After=. Also, the while loop will probably make the CPU fans blow because it re-runs systemctl without any pause.

    – PerlDuck
    Feb 3 at 16:00








  • 1





    @perlduck added a 30 second sleep in the else statement to address the inefficiency of running the script continuously. In regards to the former, the asker wanted a cronjob so I figured a simple script would be the most flexible solution, though not the most efficient. Thanks for the pointers.

    – Minty
    Feb 4 at 0:23











  • @Minty if this script is called by the cronjob mentioned by the asker, wouldn't this queue a new instance of your script every 5 minutes if the service it is waiting for is not running? If you're already using systemd, you might as well use systemd timer instead of cron and the mechanisms mentioned by PerlDuck for a pure systemd solution.

    – danzel
    Feb 4 at 10:27
















1














You could write a script using the $? variable. This variable contains the exit status of the last program; so if you run systemctl status dnsmasq and it's active, running echo $? will return 0 which means true. If it's not active, $? will return a non-0 answer, which means false.



A simple script to achieve this function would be:



#/!bin/bash
x=1
while [ "$x" != "0" ]
do
systemctl status (service to monitor)
if [ "$?" = "0" ]
then
systemctl start (service to start)
break
else
sleep 30
continue
fi
done


Line 1 = sets a variable to non-zero so a loop can be run from it



Line 2 = starts a loop that runs continuously (x can never = 0)



Line 4 = checks if a service is running, which sets $? to a zero or non-zero
value



Line 5 onward = checks if the exit status of the last command (checking if the service is running) was true or false. if it's true (zero), it starts the next service and breaks out of the loop. If it's false (non-zero) it returns to the start of the loop and runs until the selected service is running.



You could either run this script at startup or turn it into a service of its own.



You would need to run it as root, whichever your choice, since you're starting/stopping system services.






share|improve this answer





















  • 1





    You are re-implementing the systemd attributes Requires= and/or BindsTo= and/or After=. Also, the while loop will probably make the CPU fans blow because it re-runs systemctl without any pause.

    – PerlDuck
    Feb 3 at 16:00








  • 1





    @perlduck added a 30 second sleep in the else statement to address the inefficiency of running the script continuously. In regards to the former, the asker wanted a cronjob so I figured a simple script would be the most flexible solution, though not the most efficient. Thanks for the pointers.

    – Minty
    Feb 4 at 0:23











  • @Minty if this script is called by the cronjob mentioned by the asker, wouldn't this queue a new instance of your script every 5 minutes if the service it is waiting for is not running? If you're already using systemd, you might as well use systemd timer instead of cron and the mechanisms mentioned by PerlDuck for a pure systemd solution.

    – danzel
    Feb 4 at 10:27














1












1








1







You could write a script using the $? variable. This variable contains the exit status of the last program; so if you run systemctl status dnsmasq and it's active, running echo $? will return 0 which means true. If it's not active, $? will return a non-0 answer, which means false.



A simple script to achieve this function would be:



#/!bin/bash
x=1
while [ "$x" != "0" ]
do
systemctl status (service to monitor)
if [ "$?" = "0" ]
then
systemctl start (service to start)
break
else
sleep 30
continue
fi
done


Line 1 = sets a variable to non-zero so a loop can be run from it



Line 2 = starts a loop that runs continuously (x can never = 0)



Line 4 = checks if a service is running, which sets $? to a zero or non-zero
value



Line 5 onward = checks if the exit status of the last command (checking if the service is running) was true or false. if it's true (zero), it starts the next service and breaks out of the loop. If it's false (non-zero) it returns to the start of the loop and runs until the selected service is running.



You could either run this script at startup or turn it into a service of its own.



You would need to run it as root, whichever your choice, since you're starting/stopping system services.






share|improve this answer















You could write a script using the $? variable. This variable contains the exit status of the last program; so if you run systemctl status dnsmasq and it's active, running echo $? will return 0 which means true. If it's not active, $? will return a non-0 answer, which means false.



A simple script to achieve this function would be:



#/!bin/bash
x=1
while [ "$x" != "0" ]
do
systemctl status (service to monitor)
if [ "$?" = "0" ]
then
systemctl start (service to start)
break
else
sleep 30
continue
fi
done


Line 1 = sets a variable to non-zero so a loop can be run from it



Line 2 = starts a loop that runs continuously (x can never = 0)



Line 4 = checks if a service is running, which sets $? to a zero or non-zero
value



Line 5 onward = checks if the exit status of the last command (checking if the service is running) was true or false. if it's true (zero), it starts the next service and breaks out of the loop. If it's false (non-zero) it returns to the start of the loop and runs until the selected service is running.



You could either run this script at startup or turn it into a service of its own.



You would need to run it as root, whichever your choice, since you're starting/stopping system services.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 4 at 0:19

























answered Feb 3 at 15:21









MintyMinty

89329




89329








  • 1





    You are re-implementing the systemd attributes Requires= and/or BindsTo= and/or After=. Also, the while loop will probably make the CPU fans blow because it re-runs systemctl without any pause.

    – PerlDuck
    Feb 3 at 16:00








  • 1





    @perlduck added a 30 second sleep in the else statement to address the inefficiency of running the script continuously. In regards to the former, the asker wanted a cronjob so I figured a simple script would be the most flexible solution, though not the most efficient. Thanks for the pointers.

    – Minty
    Feb 4 at 0:23











  • @Minty if this script is called by the cronjob mentioned by the asker, wouldn't this queue a new instance of your script every 5 minutes if the service it is waiting for is not running? If you're already using systemd, you might as well use systemd timer instead of cron and the mechanisms mentioned by PerlDuck for a pure systemd solution.

    – danzel
    Feb 4 at 10:27














  • 1





    You are re-implementing the systemd attributes Requires= and/or BindsTo= and/or After=. Also, the while loop will probably make the CPU fans blow because it re-runs systemctl without any pause.

    – PerlDuck
    Feb 3 at 16:00








  • 1





    @perlduck added a 30 second sleep in the else statement to address the inefficiency of running the script continuously. In regards to the former, the asker wanted a cronjob so I figured a simple script would be the most flexible solution, though not the most efficient. Thanks for the pointers.

    – Minty
    Feb 4 at 0:23











  • @Minty if this script is called by the cronjob mentioned by the asker, wouldn't this queue a new instance of your script every 5 minutes if the service it is waiting for is not running? If you're already using systemd, you might as well use systemd timer instead of cron and the mechanisms mentioned by PerlDuck for a pure systemd solution.

    – danzel
    Feb 4 at 10:27








1




1





You are re-implementing the systemd attributes Requires= and/or BindsTo= and/or After=. Also, the while loop will probably make the CPU fans blow because it re-runs systemctl without any pause.

– PerlDuck
Feb 3 at 16:00







You are re-implementing the systemd attributes Requires= and/or BindsTo= and/or After=. Also, the while loop will probably make the CPU fans blow because it re-runs systemctl without any pause.

– PerlDuck
Feb 3 at 16:00






1




1





@perlduck added a 30 second sleep in the else statement to address the inefficiency of running the script continuously. In regards to the former, the asker wanted a cronjob so I figured a simple script would be the most flexible solution, though not the most efficient. Thanks for the pointers.

– Minty
Feb 4 at 0:23





@perlduck added a 30 second sleep in the else statement to address the inefficiency of running the script continuously. In regards to the former, the asker wanted a cronjob so I figured a simple script would be the most flexible solution, though not the most efficient. Thanks for the pointers.

– Minty
Feb 4 at 0:23













@Minty if this script is called by the cronjob mentioned by the asker, wouldn't this queue a new instance of your script every 5 minutes if the service it is waiting for is not running? If you're already using systemd, you might as well use systemd timer instead of cron and the mechanisms mentioned by PerlDuck for a pure systemd solution.

– danzel
Feb 4 at 10:27





@Minty if this script is called by the cronjob mentioned by the asker, wouldn't this queue a new instance of your script every 5 minutes if the service it is waiting for is not running? If you're already using systemd, you might as well use systemd timer instead of cron and the mechanisms mentioned by PerlDuck for a pure systemd solution.

– danzel
Feb 4 at 10:27


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1115237%2fcronjob-run-program-only-if-other-programm-is-already-running%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?