Launch perl script on startup

断了今生、忘了曾经 提交于 2019-12-12 03:56:03

问题


I'm currently trying to launch a perl script that is supposed to run all the time on startup.

i'm working on a CentOS server.

I added this line in /etc/rc.d/rc.local and the script won't start while it works when I type the same line from the console :

perl /svcmon/bin/svcperf.pl --svc fav01svc --interval 5 >> /dev/null &

This script is actually supposed to be launched and you have to wait for as many time as you want datas inserted in your database, but adding

>> /dev/null/& 

allows you to keep on using your machine while the script is working in background.

I don't really know what I could do because I don't get why it doesn't work...

Do you have any idea of what I could try ?

EDIT : It turns out that I managed to launch my script properly by doing this :

nohup /usr/bin/perl /svcmon/bin/svcperf.pl --svc fav01svc --interval 5 > /outputsvcperf.txt &

but it doesn't work because the PostgreSQL server isn't launched in time while it is supposed to be launched on startup and before my perl script...


回答1:


  1. In your Unix shell (where the command works) type the command: "which perl"

    This will produce an output giving a full path to perl binary. Let's say for example it returns "/bin/perl"

  2. In your rc.local script, change "perl your-command" to "/bin/perl your-command" (basically replace Perl with the fully qualifying name). (hat/tip @choroba for noticing in comments).

    This is because, on your command line, you have a $PATH set up, which lets your Unix shell find commands that reside in the path. When RC scripts run, they don't have a PATH set up for them by the shell, and therefore don't know where to search for commmands.

  3. Incidentally, this likely is NOT a problem, but change ">>/dev/null" to ">/dev/null". You don't append to a null device, you output to it.

References:

  • https://serverfault.com/questions/268674/why-does-rc-local-require-absolute-paths-how-can-i-run-a-script-at-startup-that
  • https://serverfault.com/questions/297527/how-to-test-etc-rc-d-rc-local-to-make-sure-a-command-will-start-successfully-at
  • http://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-boot-init-shutdown-run-boot.html



回答2:


Okay, here's the trick :

I created a php script which runs my command line and put it into my root's crontab so it always check if the script is running, and runs it if not.

Here's the code :

#!/usr/bin/php
<?php

    $resultat = shell_exec("ps aux | grep perl");
    if(preg_match("/svcperf.pl/", $resultat)){
        echo "Service déjà en route.";
    }else{
        echo "Service non lancé.";
        echo "\nDémarrage...";
        shell_exec("/usr/bin/perl /svcmon/bin/svcperf.pl --svc fav01svc --interval 5 > /dev/null &");
    }

?>

Thanks for the help.



来源:https://stackoverflow.com/questions/16518802/launch-perl-script-on-startup

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