定期检查是否有必要使用Crontab和Perl重新启动进程

Jav*_*ock 6 unix perl process crontab

我在perl中编写了一个简单的脚本来检查我的服务器是否正在运行.如果不是,脚本将再次启动它.这是脚本:

#!/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 &`;
}
Run Code Online (Sandbox Code Playgroud)

当我手动执行脚本时脚本工作正常,但是当我在crontab中运行脚本时,它无法找到服务器使用的dinamic库,它们位于同一个文件夹中.

Crontab条目:

*/5 * * * * /usr/bin/perl -w /full_path_to_script/autostartServer
Run Code Online (Sandbox Code Playgroud)

我想这是启动应用程序的上下文的问题.解决这个问题的聪明方法是什么?

vgo*_*anz 5

一个简单的解决方案是删除命令中的完整路径,并在执行命令之前执行"cd/path".这样它将在与库相同的文件夹中启动.代码如下所示:

#!/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 &`;
}
Run Code Online (Sandbox Code Playgroud)