Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodically check if it is necessary to restart a process with Crontab and Perl

I wrote a simple script in perl to check if my server is running. If it is not, the script will launch it again. This is the script:

#!/usr/bin/perl -w
use strict;
use warnings;
my($command, $name) = ("/full_path_to/my_server", "my_server");
if (`pidof $name`){
   print "Process is running!\n";
}
else{    
    `$command &`;
}

The scripts works perfectly when I manually execute it, but when I run it in crontab it fails to find the dinamic libraries used by the server, which are in the same folder.

Crontab entry:

*/5 * * * * /usr/bin/perl -w /full_path_to_script/autostartServer

I guess it is a problem of the context where the application is being launched. Which is the smart way to solve this?

like image 610
Jav_Rock Avatar asked Dec 03 '13 13:12

Jav_Rock


1 Answers

A simple solution is to remove the full path in the command and do a "cd /path" before executing the command. This way it will be launched in the same folder as the libraries. The code would look like this:

#!/usr/bin/perl -w

use strict;
use warnings;

my($command, $name) = ("./my_server", "my_server");
if (`pidof $name`)
{
   print "Process is running!\n";
}
else
{    
    `cd /full_path_to`;
    `$command &`;
}
like image 192
vgonisanz Avatar answered Nov 15 '22 10:11

vgonisanz