Running Perl script from crontab when using Perlbrew

I tried the following and found that it works. This is done with an unprivileged user. First find out where your perl command is located:

# which perl 

Then check the value of PERL5LIB :

 # echo $PERL5LIB 

Then, in the user crontab file, do the following:

 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 start the task at 2 a.m. and seem to find all Perl libraries correctly. My question is: complete and portable? Is there a better way?

I have seen several bash and perl scripts that should prepare the environment for executing a Perl script, but that seems to be enough. Any advice would be welcome!

EDIT . From the comments on the question, it seems like I'm using a “bad” mix of Perlbrew and local::lib . The question on how to install libraries inside a specific version of Perlbrew is answered here: How to install CPAN modules when using perlbrew? . Both cpan and cpanm will be installed under PERL5LIB when using local::lib , unless you explicitly tell them to do otherwise. cpanm also better suited to working with Perlbrew.

+7
perl cron perlbrew
source share
2 answers

The shebang ( #! ) Line of the script should point to ( perlbrew -installed) perl , which is intended to run. (This should be done as part of the installation script.) This is all you need.

 0 2 * * * /path/to/script arg1 ... 
+2
source share

If you already have several perl installations managed with perlbrew , the easiest approach is to simply use perlbrew exec to run the script. The -q and --with options allow you to disable extra output and select a specific version of perl to run script / job. Try something like:

  • perlbrew exec perl -E 'say "Hello from $]\n"' (this will show errors from earlier versions ( < 5.10 ) of perl that did not enable the -E switch by default).
  • perlbrew exec -q --with 5.26.1 perl -E 'say "Hello from $]\n"' (this will run the command and suppress the information output).
  • perlbrew exec -q --with 5.26.1 perl ~/script_from_heaven.pl (runs the script with the requested version of perl).
  • perlbrew exec -q --with 5.26.1 ~/script_from_heaven.pl (runs the script with the requested or hard-coded version in the shebang script line).

I try to explicitly install PERL5LIB and use local::lib only when I need it, or for specific users or environments where I exclusively install all CPAN modules in $HOME/perl5/lib/perl5 (say, full application deployment). Otherwise, I find that perl from perlbrew quite conveniently.


A few things I found useful : setting up an alias environment for perlbrew 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 useful / can only be used as a stable anchor #! shebang 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 cannot reference an alias using --with , for example:

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

Report this as a problem with the documentation, and an error in the to-do list.

+1
source share

All Articles