Multiple scripts in one single script - crontab

Hello all,
Hope all's well.

i'm not sure if this is possible but i have some scripts running in a crontab with different intervals, 1min, 5 min, etc.

As for a "cleaner" and better control of these scripts (as by time we will have hundred's of scripts used for the same purpose, i.e for Nagios monitoring), i was wondering if these scripts can be integrated in a master script, let's say we call it Master_cron.sh.
This master script will run every minute but, here's my dilemma, the scripts inside will still need to run with different time intervals. Do you think this can be achieved somehow?
i was thinking maybe i can use the date/time command

date +%T

to set the script to run in intervals, but am not sure i'm in the right track.

Was thinking maybe i can use an indefinite loop something like:

while sleep 1m
do
    script 1 running every 1 minute, script 2 running every 1 min, etc.
    ....
done
while sleep 5m
do
    script 1 running every 5 minuts, script 2 running every 5 min, etc.
    ....
done

For using the while am not sure if the above is a correct syntax

Note that of course i need to be careful since these scripts run in the root cron, so i cannot overwrite the root cron. The OS is Solaris 10.

Thanks in advance.

If I understand what you are asking correctly, you are saying that you have a variety of cron jobs scheduled separately using crontab . And, instead of having multiple scripts with separate entries in crontab you want to build all of those scripts into a single script that crontab will be used to run that script every minute and then that script will implement all of the features of cron and crontab to run parts of itself at appropriate intervals. If that is what you are trying to do, it sounds like you could spend a LOT of time duplicating the functionality of cron in your script, add overhead to the system and your script, and make it harder to track down issues if your scripts do not work as expected without adding any obvious benefits.

Why would you want to do this? Is it that you want to serialize all of your Nagios scripts so that they will not run concurrently (as they could when scheduled by cron )?

1 Like

Like Don Cragun said, I really don't see a point in your goal, and why you try to reinvent cron, but if you really want to do:

You are thinking of an infinite loop. Well, in your example code, you had two infinite loops serialized, which is not very sensible, because you would have to wait an infinite time until the second loop starts. Not good.

So, you would have one infinite loop. This means that this program should not run as a cron job at all. An infinite loop program runs, at least in theory, forever, and hence should not go into cron. With other words, you want to replace a cron solution by a service (daemon).

If you do not want to write a real daemon, you can of course start your program manually, but you have to do it every time after a reboot. The problem is that programs can crash, so you would need a - now again - cron job, which periodically looks after your "cron clone" and restarts it if necessary.

Do you really want to go for this?

If you want to combine your scripts, I would write one master script for each 1-minute-nagios, another master script where I invoke the 5-miniute-nagios, and so on. Then I have only few cron jobs, one for each time interval.

1 Like

Hi

You're both right. The idea was a management's request. I tend to agree that this will cause some overhead, but to go on both ways i guess it could be a good idea to setup one master script for different time intervals as rovf said.

Many thanks for you input!

This explains everything! :smiley: