2

Is it a good practice to have a bash script in a VPS with the following skeleton:

#!/bin/bash
while true; do 
    /path/to/other-bash-script-that-does-stuff.sh
    sleep 24h
done

This is to avoid touching the Cron configuration. Will this cause memory/cpu consumption problems? The script would be loaded at boot time.

UPDATE: Based on the comments, I am asking also how to check if Cron settings are working fine without waiting the time needed for a natural start of the cron job. (I am a very newbie Cron administrator)

Thank you very much for your replies!

4
  • 2
    I would say no, it's not good practice. What happens if you reboot or the program is kill -9ed? Even if it's loaded at at boot it still is going to wait 24 hours before doing task. To be mean, I could make a task to reload the system every 22 hours and you'd never get to run your code. :/ You can probably get around this by storing that information in a configuration file and checking if the current date is past the next run date. Though why do that if cron does it already? Cron is there for a reason, so unless there is a very solid reason to avoid using it, use it & save yourself pain.
    – Robert
    May 26, 2011 at 19:04
  • Why will it wait for 24hours before doing the job? The script first does the job, then waits for 24hours.
    – user82751
    May 26, 2011 at 19:09
  • 1
    Perhaps you would be better served to investigate why it doesn't work in Cron?
    – Magellan
    May 26, 2011 at 19:17
  • I've just added to the question some details about Cron
    – user82751
    May 26, 2011 at 19:29

2 Answers 2

1

While it would work, this sounds like a bad idea to me. As a system administrator, I expect that periodic executions are done through cron, so it adds complexity to the installation when it is done in a non-standard way.

If the master script was killed for any reason (executed w/o nohup and shell is terminated as one example) the process wouldn't run. Additionally, cron logs all output and the fact that it was executed. You could build all that in to your script, but why re-invent the wheel when cron is already there?

0

I use a configuration like this (though in my case it runs once, sleeps 8 hours, runs again, sleeps 8 hours, runs one last time and dies). Anecdotally I don't think its ever caused any issues. AFAIK having a sleeping process doesn't really do anything resource wise. Any reason you can't touch cron?

2
  • I don't use Cron because I need to call a Bash script and it doesn't work. I read all the related questions here around and tried everything (setting variables, paths etc.), but still doesn't with Cron. So I thought about a while true bash script
    – user82751
    May 26, 2011 at 19:07
  • 3
    @dgraziotin - I'd recommend looking into why this bash script isn't working via cron. Speaking in the long term, that's a much better way to go than implementing a "hack" like you described.
    – EEAA
    May 26, 2011 at 19:11

You must log in to answer this question.