Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Location
    France
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cron and perl script

    Hello!

    I'm having some issues running a perl script with cron. If I execute it directly everything works fine, but when cron does certain parts do not work... I've tried sending the output to dev/null, all paths are full paths yet I still can't get it to work.
    Here is a working excerpt of the script. Under cron if the process "named" is not detected the command "service named restart" is not executed (or at least so I think)...

    Code:
    #!/usr/bin/perl
    
    my @services = ( 'named' );
    
    sub bindcheck {
    
    foreach my $service (@services) {
    	my $procstatus = `/bin/ps cax | /bin/grep $service`;
    
    		if (!$procstatus) {
    			$bind=20;
    			$bindstatus="Down";
    
    		} else {
    			$bind=10;
    			$bindstatus="Running";
    
    		}
    	}
    
    }
    
    &bindcheck;
    
    if ( $bind == 20 ) {
    		system("service named restart");
    		&bindcheck;
    	}

    Thanks a lot for your precious help!

    Arthur

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts
    The first step would be to add the strict and warnings pragmas and fix the problems they point out. Those 2 pragmas should be in every Perl script you write.
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    Next, you need to add some debugging statements and send the output to a file. Make sure you open the file in a world writable directory. The debug statements should test that the script can both read and execute each of the system commands.

    As written, I would not expect the "service named restart" command to work for the following 2 reasons. 1) It lacks the path to the command. 2) The service command is a privileged command, i.e., requires root privileges which the user account that cron runs under (which I think is "nobody") most likely doesn't have.

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts

  • #4
    New to the CF scene
    Join Date
    Mar 2012
    Location
    France
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks a lot for all your help!

    I'm pretty new at this and wasn't sure as to how to write a statement that would be able to tell me if that command was executed successfully or not, but you were right, replacing "service named start" with "etc/init.d/named start" did the trick. (As for the permissions, I was under the impression that seeing as this command is executed under root's crontab that it would be run as root)

    I'll start looking into writing debuging statements, I could picture those coming in handy at some point (like yesterday )

    I don't really get the point of the "use strict" pragma however....

    Thanks!

    Arthur


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •