CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Executing script from cron.d

 



dzordzyk
Novice

Mar 13, 2010, 11:38 AM

Post #1 of 21 (1994 views)
Executing script from cron.d Can't Post

Hello,
I've weird problem. Script running from console (sudo perl check_service) works fine but from cron.d doesn't work.
Script is:

1 #!/usr/bin/perl
2
3 #
4 # Konfiguracja w pliku check_service.conf
5 #
6 #
7
8 use warnings;
9 use strict;
10 use lib '/root/';
11 use Common;
12
13 open my $config, "<", "/root/tests/check_service.conf" or die "Can not read check_service.conf: $!";
14
15 while (my $line = <$config>) {
16 chomp ($line);
17
18 next if $line =~ /^\s*#/; # komentarz
19 next if $line =~ /^\s*$/; # pusta linia
20
21 my $service = $line;
22
23 my $ps_output = `ps ax | grep "$service" | grep -v $0 | grep -v grep`;
24 if ($ps_output eq "") { # nie ma usÅ~Bugi
25 print "$service IS NOT WORKING\n";
26 Common::alert_sms($service);
27 Common::alert_mail("$service\@$Common::hostname IS NOT WORKING", "$service\@$Common::hostname IS NOT WORKING");
30 }
31 else {
32 print "$service is working\n";
33 }
34
35 }

Script check, if process is alive. If yes - nothing do. If no - send me email and sms.
In cron i've:
*/1 * * * * root perl /root/tests/check_service

So - running from console works fine, but from cron.d no. Interesting is, on another server, scripts from cron.d works fine! I don't know, what is wrong. In cron.log i see script is running ok

Thanks for reply
Regards,
dzordzyk


FishMonger
Veteran / Moderator

Mar 13, 2010, 12:44 PM

Post #2 of 21 (1991 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

I doubt that is the exact same way you execute it from the command line.

Try changing the cron job to:

Code
*/1 * * * * /root/tests/check_service



dzordzyk
Novice

Mar 13, 2010, 1:11 PM

Post #3 of 21 (1989 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post

Hello,

I changed /etc/cron.d/monitoring to

Code
*/1 * * * * root /root/tests/check_service

And It still does not work.

dzordzyk


(This post was edited by dzordzyk on Mar 13, 2010, 1:12 PM)


FishMonger
Veteran / Moderator

Mar 13, 2010, 1:20 PM

Post #4 of 21 (1985 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

Why would you expect that to work?

Does that look the same to you as what I posted?

[edit]
Hold on, that actually should have worked, if the permissions were set correctly.

What are the permission setting on that script?


(This post was edited by FishMonger on Mar 13, 2010, 1:24 PM)


dzordzyk
Novice

Mar 13, 2010, 1:24 PM

Post #5 of 21 (1982 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post


In Reply To
Why would you expect that to work?

Does that look the same to you as what I posted?


When i change it to:

Code
*/1 * * * * /root/Tanadu_scripts/tests/check_service


In log cron.log i've:

Code
Mar 13 22:24:01 xxx /usr/sbin/cron[31580]: (*system*monitoring) RELOAD (/etc/cron.d/monitoring) 
Mar 13 22:24:01 xxx cron[31580]: Error: bad command; while reading /etc/cron.d/monitoring


Edit:

Code
xxx@xxx:/root/tests$ ls -l 
total 12
-rwxr-xr-x 1 root root 956 2010-03-13 20:11 check_service
-rw-r--r-- 1 root root 303 2010-03-13 19:13 check_service.conf



(This post was edited by dzordzyk on Mar 13, 2010, 1:47 PM)


dzordzyk
Novice

Mar 13, 2010, 2:34 PM

Post #6 of 21 (1972 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post

On another server i've the same permission and works fine... I don't know, where i should look forward source of that problem.


FishMonger
Veteran / Moderator

Mar 13, 2010, 2:57 PM

Post #7 of 21 (1968 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

What error message do you get in the log file when you use the correct path to the script?

Have you tried restarting the crond daemon?


dzordzyk
Novice

Mar 13, 2010, 3:21 PM

Post #8 of 21 (1967 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post

Yes, i tried restart cron daemon.
When script runs ok, in cron.log is:


Code
Mar 14 00:10:01 ns209941 /USR/SBIN/CRON[15955]: (root) CMD (perl /root/tests/check_service)



FishMonger
Veteran / Moderator

Mar 13, 2010, 4:24 PM

Post #9 of 21 (1963 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

Ok, now you have me confused.

Does the cron job work or doesn't it?

If it doesn't, what is the log entry when it fails?


dzordzyk
Novice

Mar 13, 2010, 4:30 PM

Post #10 of 21 (1958 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post

When i run script from console:


Code
sudo perl check_service


script works fine - so, my service is down and i get sms and mail.

When scripts run from cron.d, i didn't get sms and mail - service is down too.
From cron.d script is running, but in script, it didn't get into "if":


Code
23 my $ps_output = `ps ax | grep "$service" | grep -v $0 | grep -v grep`; 
24 if ($ps_output eq "") {
25 print "$service IS NOT WORKING\n";
26 Common::alert_sms($service);
27 Common::alert_mail("$service\@$Common::hostname IS NOT WORKING", "$service\@$Common::hostname IS NOT WORKING");
30 }
31 else {
32 print "$service is working\n";
33 }



(This post was edited by dzordzyk on Mar 13, 2010, 4:31 PM)


FishMonger
Veteran / Moderator

Mar 13, 2010, 4:41 PM

Post #11 of 21 (1952 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

So, the script is actually being executed, but the backticks command is failing, is that correct?

If so then the problem is a difference in the environment. In this case it's the path environment. This is one of the main reasons you should provide full paths when shelling out.

change:

Code
$ps_output = `ps ax | grep "$service" | grep -v $0 | grep -v grep`;


to:

Code
$ps_output = `/bin/ps ax | /bin/grep "$service" | /bin/grep -v $0 | /bin/grep -v grep`;

adjust the path as needed


(This post was edited by FishMonger on Mar 13, 2010, 4:44 PM)


dzordzyk
Novice

Mar 13, 2010, 4:52 PM

Post #12 of 21 (1946 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post

Hello,

exactly - script was executed, but backticks command is falling. I change it to:


Code
 my $ps_output = `/bin/ps ax | /bin/grep "$service" | /bin/grep -v $0 | /bin/grep -v grep`;


but still does not work :(
I go to sleep, tomorrow i'll try new advise.

Thanks for all replies.


FishMonger
Veteran / Moderator

Mar 13, 2010, 5:17 PM

Post #13 of 21 (1937 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

Please be more specific. Saying "still does not work" is meaningless.

Which part of the script is not working as expected?

Are either of the print statements being executed?

If the backticks line is failing, then open a pipe to ps and make sure you include proper error handling. Assuming that part is successful, then dump out the results then process/filter the those results in the script.

Granted, this is a more verbose approach, but it's more efficient than spawning 4 external processes.


dzordzyk
Novice

Mar 13, 2010, 10:41 PM

Post #14 of 21 (1928 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post

Running script from console WORKS fine - script send me email and sms. (from console i mean i manually start script (sudo perl check_service)).
Running script from cron.d (from daemon - /etc/cron.d/monitoring) is executing, but still doesn't get into "if":


Code
$ps_output = `/bin/ps ax | /bin/grep "$service" | /bin/grep -v $0 | /bin/grep -v grep`; 
if ($ps_output eq "") {


So, script doesn't send me email and sms - but it should be, because process doesn't work: In console i put:


Code
tjurczyk@SRV001:~$ sudo /bin/ps ax | /bin/grep "s01.en" | /bin/grep -v grep 
tjurczyk@SRV001:~$


So, in program var ps_output should be empty, but in running from cron.d apparently doesn't.
Could i explained the problem clearly?

Regards


(This post was edited by dzordzyk on Mar 13, 2010, 10:42 PM)


dzordzyk
Novice

Mar 14, 2010, 4:05 AM

Post #15 of 21 (1915 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

It's really weird... I made test (modify script):

#!/usr/bin/perl

use warnings;
use strict;
use lib '/root/';
use Common;
open (MYFILE, '>>data.txt');
print MYFILE "\nXXX";
close (MYFILE);
open my $config, "<", "/root/tests/check_service.conf" or die "Can not read check_service.conf: $!";


When i run script from console (sudo perl check_service), string "XXX" was writing to data.txt. When script was running from cron.d no... Any idea?


FishMonger
Veteran / Moderator

Mar 14, 2010, 1:23 PM

Post #16 of 21 (1903 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

My linux box at home is down so I won't be able to run tests to try and duplicate the issue until I get back to work on Tuesday. However, I do have a few questions.


Quote
tjurczyk@SRV001:~$ sudo /bin/ps ax | /bin/grep "s01.en" | /bin/grep -v grep

Why are you running under sudo?

Same question here:

Quote
(from console i mean i manually start script (sudo perl check_service))


Why are you calling perl and passing the script as its arg rather than simply and directly running the script?

When running from the console like that, are you being prompted for your password?

Are you still doing it that way anywhere in the script, such as in the code that sends the email/sms?

Can you post the code in your Common module?

Have you tried moving the files to another location, such as /tmp, and run it from there instead of inside root's home directory?

Have you tried opening a pipe instead of using backticks?


FishMonger
Veteran / Moderator

Mar 16, 2010, 7:02 AM

Post #17 of 21 (1888 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

Here are the results from my test.


Code
[root@rkb-2 tests]# touch check_service.log 
[root@rkb-2 tests]# ls -l
total 20
-rw-r--r-- 1 root root 19 Mar 16 05:55 check_service.conf
-rw-r--r-- 1 root root 0 Mar 16 06:19 check_service.log
-rwxr-xr-x 1 root root 593 Mar 16 06:12 check_service.pl
[root@rkb-2 tests]# cat check_service.conf
cupsd
httpd
crond

[root@rkb-2 tests]# cat check_service.pl
#!/usr/bin/perl

use warnings;
use strict;

open my $config, '<', '/root/tests/check_service.conf'
or die "Can not read from check_service.conf: $!";

open my $log, '>>', '/root/tests/check_service.log'
or die "Can not write to check_service.log: $!";

while ( my $service = <$config> ) {
chomp ($service);

next if $service =~ /^\s*#/;
next if $service =~ /^\s*$/;

my $ps_output = `ps ax | grep "$service" | grep -v grep`;
if ($ps_output eq "") {
print $log "$service IS NOT WORKING\n";
}
else {
print $log "$service is working\n";
}
}

[root@rkb-2 tests]# cat /etc/cron.d/monitoring
* * * * * root /root/tests/check_service.pl

[root@rkb-2 tests]# tail -f check_service.log
cupsd is working
httpd IS NOT WORKING
crond is working


In a production script I'd handle all logging via Log::Log4perl and initialize the logging in a BEGIN block so that you can trap/log compile time problems.

http://search.cpan.org/~mschilli/Log-Log4perl-1.28/lib/Log/Log4perl.pm

Every phase of the script should be logged.

I suspect that the problem of not receiving the sms or email is due to the portion of your code which you haven't shown i.e, the 2 subs in the Common module.


dzordzyk
Novice

Mar 24, 2010, 8:20 AM

Post #18 of 21 (1851 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post

Hello,

Thanks for advice FishMonger.
Sorry for my silence, but i've hard time.
Problem is in others cron tasks - i have a few entries with "$service", so scripts runs from cron.d doesn't have empty string in:

Code
my $ps_output = `ps ax | grep "$service" | grep -v grep`;  
if ($ps_output eq "") {


despite the fact that the process didn't work.


FishMonger
Veteran / Moderator

Mar 24, 2010, 8:30 AM

Post #19 of 21 (1849 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

Sorry, I don't understand what you're trying to say.


dzordzyk
Novice

Mar 24, 2010, 10:04 AM

Post #20 of 21 (1844 views)
Re: [FishMonger] Executing script from cron.d [In reply to] Can't Post


In Reply To
Sorry, I don't understand what you're trying to say.


For example - in cron jobs i have also:


Code
 * * * * * www-data /home/www/s01.en//tools/backend_cronjob.sh


So when i check service "s01.en" with "grep", string:


Code
my $ps_output = `ps ax | grep "$service" | grep -v $0 | grep -v grep`;


string ps_output wasn't empty, because "ps ax" see other tasks like string "s01.en" running from cron.d.


(This post was edited by dzordzyk on Mar 24, 2010, 10:05 AM)


FishMonger
Veteran / Moderator

Mar 24, 2010, 10:27 AM

Post #21 of 21 (1839 views)
Re: [dzordzyk] Executing script from cron.d [In reply to] Can't Post

Based on what you're describing, "s01.en" is not a running service that has a pid number, it's a directory within the path to a running program.

If that's true, then obviously you're grepping for the wrong string.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives