我用 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 &`;
}
当我手动执行脚本时,脚本运行良好,但是当我在 crontab 中运行它时,它无法找到服务器使用的动态库,它们位于同一文件夹中。
Crontab 条目:
*/5 * * * * /usr/bin/perl -w /full_path_to_script/autostartServer
我想这是启动应用程序的上下文的问题。哪个是解决这个问题的聪明方法?
最佳答案
一个简单的解决方案是删除命令中的完整路径并在执行命令之前执行“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 &`;
}
关于perl - 定期检查是否需要使用 Crontab 和 Perl 重新启动进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20352093/