How to start a never ending script at startup












3















I'm trying to automatically run a never ending script (so it doesn't return 0 on exit) at startup on my headless ubuntu 12.04 server with no GUI.



I have tried @reboot nohup /home/luke/netup.sh & in crontab and the script doesn't work properly although it appears to run.
I have tried update-rc.d netup.sh defaults, the script started but still didn't run properly and most of the other programs that are supposed to auto start didn't start.



The script attempts to monitor and record internet outages and contains a while-do loop. It works when logged in to the server and started manually.



Here is the script



#!/bin/bash
#
# Script to monitor internet up time

echo "Server started" `date "+%F %T"` >> /home/luke/netup.log

START=0

while [ 1 ] ; do # continuous loop

#------------------------------------------------------------------------
/bin/ping -q 8.8.8.8 -c1 1>/dev/null 2>/dev/null # ping test
PING=$?
#------------------------------------------------------------------------

if [ $PING = 0 ]; then # ping success
if [ $START -ne 0 ]; then # was down
END=$(date +%s)
TIME=$(($END - $START))
START=0
let TIME=($TIME/60) #convert seconds to minutes
echo "Failed" $FAIL_TIME "for" $TIME "minutes" >> /home/luke/netup.log

fi

else # ping failure
if [ $START -eq 0 ]; then # was up
START=$(date +%s)
FAIL_TIME=$(date "+%F %T")
fi

fi

#------------------------------------------------------------------------
if [ $PING = 0 ]; then # wait

sleep 60
else
sleep 10
fi

done









share|improve this question

























  • Did you try to call your script from /etc/rc.local?

    – Eric Carvalho
    Sep 5 '12 at 16:56
















3















I'm trying to automatically run a never ending script (so it doesn't return 0 on exit) at startup on my headless ubuntu 12.04 server with no GUI.



I have tried @reboot nohup /home/luke/netup.sh & in crontab and the script doesn't work properly although it appears to run.
I have tried update-rc.d netup.sh defaults, the script started but still didn't run properly and most of the other programs that are supposed to auto start didn't start.



The script attempts to monitor and record internet outages and contains a while-do loop. It works when logged in to the server and started manually.



Here is the script



#!/bin/bash
#
# Script to monitor internet up time

echo "Server started" `date "+%F %T"` >> /home/luke/netup.log

START=0

while [ 1 ] ; do # continuous loop

#------------------------------------------------------------------------
/bin/ping -q 8.8.8.8 -c1 1>/dev/null 2>/dev/null # ping test
PING=$?
#------------------------------------------------------------------------

if [ $PING = 0 ]; then # ping success
if [ $START -ne 0 ]; then # was down
END=$(date +%s)
TIME=$(($END - $START))
START=0
let TIME=($TIME/60) #convert seconds to minutes
echo "Failed" $FAIL_TIME "for" $TIME "minutes" >> /home/luke/netup.log

fi

else # ping failure
if [ $START -eq 0 ]; then # was up
START=$(date +%s)
FAIL_TIME=$(date "+%F %T")
fi

fi

#------------------------------------------------------------------------
if [ $PING = 0 ]; then # wait

sleep 60
else
sleep 10
fi

done









share|improve this question

























  • Did you try to call your script from /etc/rc.local?

    – Eric Carvalho
    Sep 5 '12 at 16:56














3












3








3








I'm trying to automatically run a never ending script (so it doesn't return 0 on exit) at startup on my headless ubuntu 12.04 server with no GUI.



I have tried @reboot nohup /home/luke/netup.sh & in crontab and the script doesn't work properly although it appears to run.
I have tried update-rc.d netup.sh defaults, the script started but still didn't run properly and most of the other programs that are supposed to auto start didn't start.



The script attempts to monitor and record internet outages and contains a while-do loop. It works when logged in to the server and started manually.



Here is the script



#!/bin/bash
#
# Script to monitor internet up time

echo "Server started" `date "+%F %T"` >> /home/luke/netup.log

START=0

while [ 1 ] ; do # continuous loop

#------------------------------------------------------------------------
/bin/ping -q 8.8.8.8 -c1 1>/dev/null 2>/dev/null # ping test
PING=$?
#------------------------------------------------------------------------

if [ $PING = 0 ]; then # ping success
if [ $START -ne 0 ]; then # was down
END=$(date +%s)
TIME=$(($END - $START))
START=0
let TIME=($TIME/60) #convert seconds to minutes
echo "Failed" $FAIL_TIME "for" $TIME "minutes" >> /home/luke/netup.log

fi

else # ping failure
if [ $START -eq 0 ]; then # was up
START=$(date +%s)
FAIL_TIME=$(date "+%F %T")
fi

fi

#------------------------------------------------------------------------
if [ $PING = 0 ]; then # wait

sleep 60
else
sleep 10
fi

done









share|improve this question
















I'm trying to automatically run a never ending script (so it doesn't return 0 on exit) at startup on my headless ubuntu 12.04 server with no GUI.



I have tried @reboot nohup /home/luke/netup.sh & in crontab and the script doesn't work properly although it appears to run.
I have tried update-rc.d netup.sh defaults, the script started but still didn't run properly and most of the other programs that are supposed to auto start didn't start.



The script attempts to monitor and record internet outages and contains a while-do loop. It works when logged in to the server and started manually.



Here is the script



#!/bin/bash
#
# Script to monitor internet up time

echo "Server started" `date "+%F %T"` >> /home/luke/netup.log

START=0

while [ 1 ] ; do # continuous loop

#------------------------------------------------------------------------
/bin/ping -q 8.8.8.8 -c1 1>/dev/null 2>/dev/null # ping test
PING=$?
#------------------------------------------------------------------------

if [ $PING = 0 ]; then # ping success
if [ $START -ne 0 ]; then # was down
END=$(date +%s)
TIME=$(($END - $START))
START=0
let TIME=($TIME/60) #convert seconds to minutes
echo "Failed" $FAIL_TIME "for" $TIME "minutes" >> /home/luke/netup.log

fi

else # ping failure
if [ $START -eq 0 ]; then # was up
START=$(date +%s)
FAIL_TIME=$(date "+%F %T")
fi

fi

#------------------------------------------------------------------------
if [ $PING = 0 ]; then # wait

sleep 60
else
sleep 10
fi

done






startup scripts cron






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 '12 at 16:51









Eric Carvalho

42.1k17115147




42.1k17115147










asked Sep 5 '12 at 13:09









LukeLuke

185




185













  • Did you try to call your script from /etc/rc.local?

    – Eric Carvalho
    Sep 5 '12 at 16:56



















  • Did you try to call your script from /etc/rc.local?

    – Eric Carvalho
    Sep 5 '12 at 16:56

















Did you try to call your script from /etc/rc.local?

– Eric Carvalho
Sep 5 '12 at 16:56





Did you try to call your script from /etc/rc.local?

– Eric Carvalho
Sep 5 '12 at 16:56










1 Answer
1






active

oldest

votes


















0














Rather than setting up a script to run constantly at startup, why not change it to run using cron? Since you are telling it to sleep for 60 seconds between runs anyway, using cron to run a script without a while loop once a minute would make more sense and be simpler to manage.



You might also be interested in the answers for this question on serverfault:



https://serverfault.com/questions/49082/can-i-run-a-cron-job-more-frequently-than-every-minute






share|improve this answer


























  • That sounds like a great idea. I'll have to re-write the script and maybe put the contents of START variable into a text file and create a seperate cron job to record the server start time, but probably worth the effort. Thanks

    – Luke
    Sep 10 '12 at 17:46













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%2f184352%2fhow-to-start-a-never-ending-script-at-startup%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









0














Rather than setting up a script to run constantly at startup, why not change it to run using cron? Since you are telling it to sleep for 60 seconds between runs anyway, using cron to run a script without a while loop once a minute would make more sense and be simpler to manage.



You might also be interested in the answers for this question on serverfault:



https://serverfault.com/questions/49082/can-i-run-a-cron-job-more-frequently-than-every-minute






share|improve this answer


























  • That sounds like a great idea. I'll have to re-write the script and maybe put the contents of START variable into a text file and create a seperate cron job to record the server start time, but probably worth the effort. Thanks

    – Luke
    Sep 10 '12 at 17:46


















0














Rather than setting up a script to run constantly at startup, why not change it to run using cron? Since you are telling it to sleep for 60 seconds between runs anyway, using cron to run a script without a while loop once a minute would make more sense and be simpler to manage.



You might also be interested in the answers for this question on serverfault:



https://serverfault.com/questions/49082/can-i-run-a-cron-job-more-frequently-than-every-minute






share|improve this answer


























  • That sounds like a great idea. I'll have to re-write the script and maybe put the contents of START variable into a text file and create a seperate cron job to record the server start time, but probably worth the effort. Thanks

    – Luke
    Sep 10 '12 at 17:46
















0












0








0







Rather than setting up a script to run constantly at startup, why not change it to run using cron? Since you are telling it to sleep for 60 seconds between runs anyway, using cron to run a script without a while loop once a minute would make more sense and be simpler to manage.



You might also be interested in the answers for this question on serverfault:



https://serverfault.com/questions/49082/can-i-run-a-cron-job-more-frequently-than-every-minute






share|improve this answer















Rather than setting up a script to run constantly at startup, why not change it to run using cron? Since you are telling it to sleep for 60 seconds between runs anyway, using cron to run a script without a while loop once a minute would make more sense and be simpler to manage.



You might also be interested in the answers for this question on serverfault:



https://serverfault.com/questions/49082/can-i-run-a-cron-job-more-frequently-than-every-minute







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 13 '17 at 12:14









Community

1




1










answered Sep 5 '12 at 17:19









ImaginaryRobotsImaginaryRobots

7,20042636




7,20042636













  • That sounds like a great idea. I'll have to re-write the script and maybe put the contents of START variable into a text file and create a seperate cron job to record the server start time, but probably worth the effort. Thanks

    – Luke
    Sep 10 '12 at 17:46





















  • That sounds like a great idea. I'll have to re-write the script and maybe put the contents of START variable into a text file and create a seperate cron job to record the server start time, but probably worth the effort. Thanks

    – Luke
    Sep 10 '12 at 17:46



















That sounds like a great idea. I'll have to re-write the script and maybe put the contents of START variable into a text file and create a seperate cron job to record the server start time, but probably worth the effort. Thanks

– Luke
Sep 10 '12 at 17:46







That sounds like a great idea. I'll have to re-write the script and maybe put the contents of START variable into a text file and create a seperate cron job to record the server start time, but probably worth the effort. Thanks

– Luke
Sep 10 '12 at 17:46




















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%2f184352%2fhow-to-start-a-never-ending-script-at-startup%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

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

File:DeusFollowingSea.jpg