Running a Perl script from crontab when you use Perlbrew

I have tried the following and find it to work. This is done with a non-privileged user. First find out where your perl command is:

# which perl

Then check the value of PERL5LIB :

# echo $PERL5LIB

Then, at the crontab file of the user, do something like:

MAILTO=<my email address for the jobs output>
HOME=/home/myhome
PERL5LIB=/home/myhome/perl5/lib/perl5

0 2 * * * $HOME/<rest of path to perl>/perl $HOME/<path to my perl script> arg1 ...

This will run a job at 2am and seems to find all Perl libs correctly. My question is: is this complete and portable? Is there a better way?

I have seen a number of bash and perl scripts out there that are supposed to prepare the environment for the execution of a Perl script, but this seems to suffice. Any advice will be welcome!

EDIT : From the comments to the question, it seems that I am using a "bad" mixture of Perlbrew and local::lib . The way to make sure libraries get installed inside a particular Perlbrew version is answered here: How do I install CPAN modules while using perlbrew?. Both cpan and cpanm will install under PERL5LIB when you are using local::lib unless you explicitly tell them to do otherwise. Also cpanm seems to be better suited to working along with Perlbrew.


The shebang ( #! ) line of the script should point to the ( perlbrew -installed) perl it is meant to run under. (This should be done as part of installing the script.) That's all you need.

0 2 * * * /path/to/script arg1 ...

If you already have multiple perl installations managed with perlbrew the easiest approach is to just use perlbrew exec to run your script. The -q and --with options allow you to silence superfluous output and select the specific version of perl to run the script/job. Try something like:

  • perlbrew exec perl -E 'say "Hello from $]n"' (this will show errors from older versions ( < 5.10 ) of perl that don't have the -E switch enabled by default).
  • perlbrew exec -q --with 5.26.1 perl -E 'say "Hello from $]n"' (this will run the command and suppress informational output).
  • perlbrew exec -q --with 5.26.1 perl ~/script_from_heaven.pl (runs the script with the perl version requested).
  • perlbrew exec -q --with 5.26.1 ~/script_from_heaven.pl (runs the script with the perl version requested or hard-coded in the script's shebang line).
  • I tend to explicitly set PERL5LIB and use local::lib only when I need them or for certain users or environments where I exclusively install all CPAN modules in $HOME/perl5/lib/perl5 (a full application deployment, say). Otherwise I find running perl from perlbrew pretty convenient.


    A couple of things I've found helpful : setting an alias for perlbrew environments that you want to keep stable for a particular use can be a useful way to manage multiple perls:

     ~/$ perlbrew alias create perl-5.24.0 stable-cronperl
     ~/$ perlbrew list
     perl-5.8.9
     perl-5.10.1
     perl-5.24.0
     cperl-cperl-5.26.1
     stable-cronperl (5.24.0)
     perl-5.26.1
    

    NB : however the alias is only useful/useable as a stable #! shebang anchor for use at the top of your scripts if you want to make them executable:

    #!/home/cronic/perl5/perlbrew/perls/stable-cronperl/bin/perl
    

    You can't refer to an alias using --with for example:

    perlbrew exec --with stable-cronperl ~/smart_comments.pl

    Reporting this as either a documentation issue or a bug is on my to do list.

    链接地址: http://www.djcxy.com/p/40878.html

    上一篇: @Retention注释的递归使用,它怎么可能?

    下一篇: 使用Perlbrew时,从crontab运行Perl脚本