bash script while true + sleep 24 hours + single command VS Cron

5,533

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?

Share:
5,533

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years

    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!

    • Admin
      Admin about 13 years
      Why will it wait for 24hours before doing the job? The script first does the job, then waits for 24hours.
    • Magellan
      Magellan about 13 years
      Perhaps you would be better served to investigate why it doesn't work in Cron?
    • Admin
      Admin about 13 years
      I've just added to the question some details about Cron
  • Esa Jokinen
    Esa Jokinen about 13 years
    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
  • EEAA
    EEAA about 13 years
    @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.