Running perl in cron - missing modules [solved]

I’ve been trying to run an EPG update script via cron and I can’t get it to work. The setup is that cron calls a bash script I wrote that calls the perl script “zap2xml.pl” It works fine if I run the bash script in my shell, but cron is unable to find the perl modules when I run it, probably due to the stripped-down env that it runs in. I’ve tried the following:

  • Installed cron from app store
  • All files are executable (chmod +x) with wide-open permissions (chmod 777)
  • Used absolute paths for commands and files
  • Tried to run perl directly from crontab
  • Tried setting path variables in cron directly
  • Everything else i can think of (I’m not very adept at linux)

I am printing out my user PATH definition with:

perl -le 'print for grep {$_ ne q{.}and -d} @INC'

and plugging those paths into my bash script (see below)
Here is my current crontab:

15 3 * * * /bin/bash /media/zap2xml/run_zap2xml.sh >> /media/zap2xml/cron.log

Here is the run_zap2xml.sh file

#!/bin/bash
PATH=$PATH:/home/osmc/perl5/lib/perl5/5.20.2/arm-linux-gnueabihf-thread-multi-64int:/home/osmc/perl5/lib/perl5/5.20.2:/home/osmc/perl5/lib/perl5/arm-linux-gnueabihf-thread-multi-64int:/home/osmc/perl5/lib/perl5:/etc/perl:/usr/lib/arm-linux-gnueabihf/perl5/5.20:/usr/share/perl5:/usr/lib/arm-linux-gnueabihf/perl/5.20:/usr/share/perl/5.20:/home/osmc/perl5/lib/perl5/5.20.0
echo "set path"
echo $PATH
cd /media/zap2xml
usr/bin/perl /media/zap2xml/zap2xml.pl -C /media/zap2xml/zap2xml_settings.txt

The cron.log file output by cron prints the proper PATH, with all the stuff I added appended, but the perl script still complains about missing modules. It doesn’t tell me which ones are missing, but like I said, it runs fine in my own user env, which I’m trying to reproduce with the PATH definition.

Any ideas? Thanks!

Are you sure your perl stuff is under /home/osmc/perl5/ ? That seems an unusual place to put it…

I can see two bugs in your shell script right away.

  1. You have set PATH but you have not exported it.

In bash if you assign a variable such as PATH=$PATH:/home/osmc/blah, that variable only exists in the current context as a local variable, it does not change the environment of the process or any child processes which are launched from the shell, such as your perl script…

So you either change the line to export PATH=$PATH:/home/osmc/blah or just add a line export PATH between your path assignment and where you call perl. I would normally use the former as it is tidier. Using export as an assignment both creates a variable and sets it in the environment in one statement.

http://ss64.com/bash/export.html

  1. You seem to be missing a forward slash off the beginning of /usr/bin/perl …
1 Like

Thanks for the response. Sorry, I know that retyping code is never as smart as copying and pasting. The missing forward slash is a typo, it’s definitely in there.

As for the export command, I did have that in there last night and it spit out an error in the log file that said it was an invalid command. I did not have an absolute path to it (since I’m not sure where it is). It looked like this:

#/bin/bash
PATH=$PATH:/home/osmc/perl5/lib/perl5/5.20.2/arm-linux-gnueabihf-thread-multi-64int:/home/osmc/perl5/lib/perl5/5.20.2:/home/osmc/perl5/lib/perl5/arm-linux-gnueabihf-thread-multi-64int:/home/osmc/perl5/lib/perl5:/etc/perl:/usr/lib/arm-linux-gnueabihf/perl5/5.20:/usr/share/perl5:/usr/lib/arm-linux-gnueabihf/perl/5.20:/usr/share/perl/5.20:/home/osmc/perl5/lib/perl5/5.20.0
export PATH
cd /media/zap2xml
/usr/bin/perl /media/zap2xml/zap2xml.pl -C /media/zap2xml/zap2xml_settings.txt

I stripped it out because it wasn’t working.

As for the location of the perl libraries, I don’t know what to say. I might have done something goofy when installing them. But like I said, I just grabbed the output from perl -le ‘print @INC’ and plopped it in the PATH.

export is a bash built in, so there is no way it should say that its an invalid command. :wink: Paste the exact error message if possible.

It definitely won’t work if you don’t use export as the perl script will not see your modified version of PATH in its environment - it will still get the standard PATH.

Ok, thanks. I’ll try it again tonight and get back to you with the results. As far as the actual PATH definition and syntax, is it possible I’m missing something? If I do perl -le ‘print @INC’ is that a comprehensive list?

Thanks again

I’m not a perl user unfortunately, hopefully someone else might know.

You can pass PERL5LIB to add a custom directory to @INC array.

1 Like

I wish I was smart enough to know how to do that. :stuck_out_tongue:
Can you explain it a little bit? I did try to define PERL5LIB in the bash script at one point, same way as the PATH definition, but it didn’t work either.

That should be all that’s needed

Make sure you are running cron tab as regular user

OK, thanks. I’ll let you know how it goes.

edit: to run the crontab, all I’m doing is logging into an ssh shell with the standard oscm/osmc login credentials and running crontab -e

That is correct. That will edit the osmc users crontab, which will run scripts as the osmc user.

OK, seems to be working. New approach, I printed env to a text file:

env >> /media/zap2xml/env.txt

I just grabbed everything that looked like a perl path (along with the actual PATH path) and smashed it in the bash script, which looks like this now:

#!/bin/bash
echo "starting script"
export PATH=/home/osmc/perl5/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/sbin:/usr/sbin:/usr/osmc/bin:/opt/vc/bin:$PATH
export PERL_LOCAL_LIB_ROOT=/home/osmc/perl5
export PERL5LIB=/home/osmc/perl5/lib/perl5
export PERL_MB_OPT=--install_base "/home/osmc/perl5"
export PERL_LOCAL_LIB_ROOT=/home/osmc/perl5
export PERL_MM_OPT=INSTALL_BASE=/home/osmc/perl5
echo "defined and exported user paths"
/bin/sleep 3s
echo "wait 3 seconds before continuing"
#script to run zap2xml perl script
cd /media/zap2xml
echo "changed directory"
/usr/bin/perl -l /media/zap2xml/zap2xml.pl -C /media/zap2xml/zap2xml_settings.txt
echo "ran perl script"

Don’t know why export worked today and not yesterday… maybe it didn’t like being on a different line all by itself? Who knows. If anybody is interested, I can go in and turn off individual path and library variables until I find the ones that fix it, but at this point, I’m just happy it’s working.

Thanks again DBMandrake and Sam for the help. Much appreciated!