bash script while true + sleep 24 hours + single command VS Cron
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?
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on September 18, 2022Comments
-
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 about 13 yearsWhy will it wait for 24hours before doing the job? The script first does the job, then waits for 24hours.
-
Magellan about 13 yearsPerhaps you would be better served to investigate why it doesn't work in Cron?
-
Admin about 13 yearsI've just added to the question some details about Cron
-
-
Esa Jokinen about 13 yearsI 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 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.