LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Put each daily cron task in a separate script or put them all in one script? (https://www.linuxquestions.org/questions/linux-newbie-8/put-each-daily-cron-task-in-a-separate-script-or-put-them-all-in-one-script-908818/)

veeruk101 10-18-2011 02:07 PM

Put each daily cron task in a separate script or put them all in one script?
 
I need to run some tasks in anacron (not regular cron), and apparently the way it works is I put these scripts in /etc/cron.daily and anacron will take care of the rest. I have two options - put every task in one big bash script, or put each task in its own script. In general terms, what things should I be considering when making the decision of which way to go about it?

One thing I see that might make it better to go with one big script is I can ensure some tasks are run before others. Or will the scripts in /etc/cron.daily run in alphabetical order? In /etc/anacrontab, the following is included, and I can't tell whether run-parts will execute them in alphabetical order, and if so whether this would be a nuance of this particular system or whether that behavior is standard:

Code:

1          5        cron.daily                nice run-parts /etc/cron.daily
Apart from that consideration, what other things should I be looking at in terms of how to organize all my cron tasks? How do you do it in your systems? Any best practices, tips, or things to think about you could suggest would be great.

xjonquilx 10-18-2011 03:43 PM

Well, there's several ways you could go about this.

You could put them in separate files so you only have to run the ones you need at the moment.

You could put them in one file so they all run at once.

And one option I didn't see considered: You could put them in one file with a text menu so you can select which ones you want to run.

Any of these methods will work, it just depends on what you want to do. And you don't seem very certain that you know what you want to do so that would be your first step.

Sorry I can't help more than this. I'm not a system administrator.

tronayne 10-18-2011 03:59 PM

Even though anacron behaves differently than does cron, the idea is more-or-less the same -- if you need to run some group of tasks where subsequent tasks depend upon some prior task being completed, you want to place those task in sequence in one shell program (script). Things that do not depend upon some prior task completing can stand alone.

Something to be aware of in any case, ancron or cron, is that the shell that cron jobs run in by default is extremely limited. If you are going to be executing a program found in /usr/local/bin, for example, it frequently is necessary to add that directory to the PATH environment as well as any libraries found in /usr/local/lib to the LD_LIBRARY_PATH environment variable; e.g., at the top of your shell program
Code:

export PATH=${PATH}:/usr/local/bin
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib

You would do the same for any other directories containing executables you would be using in the job.

One does wonder, though, why you would be using anacon rather than just plain old cron? It appears that the only real benefit is that a cron job will be run by anacron if the system has been shut down for some reason when the system is restarted. If you don't expect (or experience) a lot of powered-off time...

Anyway.

Hope this helps some.

veeruk101 10-19-2011 06:32 AM

I'm using anacron because it's a laptop not a server that can be expected to remain on at all times. If I use cron instead of anacron, most of my cron runs won't actually run.

So are the scripts in the cron.daily directories (as executed by run-parts) always executed in alphabetical order and, if so, is this behaviour standard and can be relied upon?

tronayne 10-19-2011 07:10 AM

Quote:

Originally Posted by veeruk101 (Post 4502405)
I'm using anacron because it's a laptop not a server that can be expected to remain on at all times. If I use cron instead of anacron, most of my cron runs won't actually run.

Well, that's what the purpose of it is, so sounds like you've got the right tool (I wasn't being critical, just curios!).
Quote:

So are the scripts in the cron.daily directories (as executed by run-parts) always executed in alphabetical order and, if so, is this behaviour standard and can be relied upon?
That I don't know (but it's quite possible if it uses some form of ls to get the file names). Best bet? Make up a few that just just echo something like "this is a," "this is b" and so on and see what happens (ought to be a log, eh?).


All times are GMT -5. The time now is 02:01 AM.