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

cron, perl, process, unix

Solution

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 &`;
}

Problem

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?

Original source