Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-13-2012, 05:18 PM   PM User | #1
rthur
New to the CF scene

 
Join Date: Mar 2012
Location: France
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
rthur is an unknown quantity at this point
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
rthur is offline   Reply With Quote
Old 04-14-2012, 02:36 PM   PM User | #2
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
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.
FishMonger is offline   Reply With Quote
Old 04-14-2012, 02:52 PM   PM User | #3
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Here are some useful modules related to your task.

IPC::Run - system() and background procs w/ piping, redirs, ptys (Unix, Win32)
Unix::Process - Perl extension to get pid info from (/bin/ps).
Sudo - Perl extension for running a command line sudo
FishMonger is offline   Reply With Quote
Old 04-15-2012, 11:22 AM   PM User | #4
rthur
New to the CF scene

 
Join Date: Mar 2012
Location: France
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
rthur is an unknown quantity at this point
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
rthur is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 PM.


Advertisement
Log in to turn off these ads.