Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to use cron to run three scripts. I want each file to run only if the previous one or two have ended without any errors. I was thinking of using a system at the end of the first script to call the second and then testing to see its success. Then ofcourse repeating this for the third script. I will be running my cron as root. Is this the best way? safest way?

Replies are listed 'Best First'.
Re: safe to use system
by Plankton (Vicar) on Sep 16, 2003 at 19:32 UTC
    Maybe you could have cron entry that looks like this ...
    30 * * * * script1 && script2 && script3
    Provided that scripts do exit 0 when they complete successfully and exit something other than zero when there is an error.
    Plankton: 1% Evil, 99% Hot Gas.
Re: safe to use system
by tcf22 (Priest) on Sep 16, 2003 at 19:43 UTC
    You could always do it with a 4th perl script, that runs the other 3.
    #! /usr/bin/perl -w use strict; system('perl script1.pl') && exit(1); system('perl script2.pl') && exit(2); system('perl script3.pl') && exit(3); exit(0);

    - Tom

Re: safe to use system
by Limbic~Region (Chancellor) on Sep 16, 2003 at 19:40 UTC
    Anonymous Monk,
    Not really a Perl question, but here goes.

    Running these scripts from cron as root is usually not a good idea. You want to make sure the ownership/permissions on the scripts themselves are correct. Depending on what you mean by "ended without errors", you might be able to just get away with checking the exit status. On the other hand, the script may complete but still encounter problems.

    If this is the case and you want to be sure it is safe to continue, here is how I would do it:

  • Have cron delete the "ok-#" files before calling the first script
  • Have the first script's last item of business be to create the "ok-1" file
  • Have the second script exit if the "ok-1" script doesn't exist
  • Have the second script's last item of business be to create the "ok-2" file
  • Have the third script exit if the "ok-2" script doesn't exist.
  • Have the third script's last item of business be to create the "ok-3" file
  • Have cron check for the existance of all 3 "ok" files and email you with either "success" or "failure"

    Now this method is not without gotchas. Perhaps the script ran fine but couldn't create the "ok" file. Maybe someone deleted it during the window of opportunity (after the first script created it and the next script read it).

    Cheers - L~R

Re: safe to use system
by Roger (Parson) on Sep 17, 2003 at 01:46 UTC
    One suggestion, hardcode any path to programs/scripts that you want to access in the script, just in case because you are running the script as root. And make sure only root can modify the script.