1

I want to schedule my Perl code to be run every day at a specific time. so I put the below code in bash file:

Automate.sh

    #!/bin/sh
    perl /tmp/Taps/perl.pl

The schedule has been specified in below path:

10 17 * * * sh /tmp/Taps/Automate.sh > /tmp/Taps/result.log

When the time arrived to 17:10 the .sh file hasn't been running. however, when I run ./Automate.sh (manually) it is running and I see the result. I don't know what is the problem.

Perl Code

#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
use XML::Dumper;
use TAP3::Tap3edit;
$Data::Dumper::Indent=1;
$Data::Dumper::Useqq=1;
my $dump = new XML::Dumper;
use File::Basename;


my $perl='';
my $xml='';
my $tap3 = TAP3::Tap3edit->new();

   foreach my $file(glob '/tmp/Taps/X*')
   {
   $files= basename($file);
   $tap3->decode($files) || die $tap3->error;
    }

my $filename=$files.".xml\n";
$perl = $tap3->structure;
$dump->pl2xml($perl, $filename);
print "Done \n";

error:

No such file or directory for file X94 at /tmp/Taps/perl.pl line 22.
X94.xml
8
  • i did it but it is not running and i have an empty log file Commented Aug 31, 2020 at 13:07
  • 2
    @Saher : cron should send you the stderr by mail. Hence you should get some error messages. Alternatively, you could do a 2>&1 and collect the stderr inside your result.log. I would also use -x with sh. Then, the log file at least can't be empty anymore. And I would specify the full path to sh and perl. Commented Aug 31, 2020 at 13:10
  • Create crontab job where you put a command env > /tmp/env_data and once the cronjob is run you will see all variables set in crontab. man crontab gives sufficient information for understanding what required to set to run programs. Commented Sep 1, 2020 at 0:26
  • Why are you using basename? Consider what happens if your pwd is / Commented Sep 6, 2020 at 12:05
  • $files is not declared anywhere in the code you show. With use strict, this code would produce a fatal error Global symbol "$files" requires explicit package. Commented Sep 6, 2020 at 12:13

3 Answers 3

5

foreach my $file(glob 'Taps/X*') -- when you're running from cron, your current directory is /. You'll want to provide the full path to that Taps directory. Also specify the output directory for Out.xml

Sign up to request clarification or add additional context in comments.

1 Comment

I revised my code as above and now i get above error: No such file or directory for file X94 at /tmp/Taps/perl.pl line 22. X94.xml
4

Cron uses a minimal environment and a short $PATH, which may not necessarily include the expected path to perl. Try specifying this path fully. Or source your shell settings before running the script.

3 Comments

the specified file naming in crontab is not complete? I used short path but the result hasnt been changed!
I mean something like using /path/to/perl instead of just perl.
I revise my code and get above error. No such file or directory for file X94 at /tmp/Taps/perl.pl line 22. X94.xml
2

There are a lot of things that can go wrong here. The most obvious and certain one is that if you use a glob to find the file in directory "Taps", then remove the directory from the file name by using basename, then Perl cannot find the file. Not quite sure what you are trying to achieve there. The file names from the glob will be for example Taps/Xfoo, a relative path to the working directory. If you try to access Xfoo from the working directory, that file will not be found (or the wrong file will be found).

This should also (probably) lead to a fatal error, which should be reported in your error log. (Assuming that the decode function returns a false value upon error, which is not certain.) If no errors are reported in your error log, that is a sign the program does not run at all. Or it could be that decode does not return false on missing file, and the file is considered to be empty.

I assume that when you test the program, you cd to /tmp and run it, or your "Taps" directory is in your home directory. So you are making assumptions about where your program looks for the files. You should be certain where it looks for files, probably by using only absolute paths.

Another simple error might be that crontab does not have permission to execute the file, or no read access to "Taps".

Edit:

Other complications in your code:

  • You include Data::Dumper, but never actually use that module.
  • $xml variable is not used.
  • $files variable not declared (this code would never run with use strict)
  • Your $files variable is outside your foreach loop, which means it will only run once. Since you use glob I assumed you were reading more than one file, in which case this solution will probably not do what you want. It is also possible that you are using a glob because the file name can change, e.g. X93, X94, etc. In that case you will read the last file name returned by the glob. But this looks like a weak link in your logic.
  • You add a newline \n to a file name, which is strange.

2 Comments

I tried to make relative changes and revise the code. But I got error as it shows above.
No such file or directory error is probably because you use basename on the relative file name, like I explained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.