perl run background job and force job to finish?

对着背影说爱祢 提交于 2019-12-06 06:12:47

This works for me:

$SIG{CHLD}="IGNORE";
my $cmd = "top -b -d 60 -n 300 -u $ENV{USER} >> logfile";
if (my $logger_pid=fork) {
    # parent
    do_something_for_1to2_hours();
    kill 15, $logger_pid;
} else {
    # child (logger)
    exec($cmd);
}

Usually double fork is used so that you don't have to worry about reaping child processes. Since you want to control the child process, you can just do a single fork, which will give you the process id which you can then use to kill your child process using kill.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!