6

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?

3
  • Maybe consider putting it in inittab so it gets respawned automagically. Dec 3, 2013 at 13:11
  • Check for non blocking exclusive file lock; it does not depend on external commands and it's portable.
    – mpapec
    Dec 3, 2013 at 13:13
  • 3
    Instead of reinventing the wheel with your own process watcher, you should have a look at one of the existing ones which solve all the edge cases of hanging processes like systemd, runit, daemontools, god, monit, ... Nowadays, your distribution should ship with at least one of those (and might even use it by default). Dec 3, 2013 at 13:15

1 Answer 1

5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.