-1

i have created the perl script in which i am connecting to vsql and running queries. when i run the script mannually, it is creating output files as expected. but when i set this script in crontab then output file is not generating. perl script is given below

#!/usr/bin/perl
$timenow = `date "+%H_%M"`;
chomp($timenow);
$cmd = "/opt/vertica/bin/vsql -d xxxx-U xxxxx -w xxxxx -F \$'--FSEP--' -At -o dumpfile_" . $timenow . ".txt -c \"SELECT CURRENT_TIMESTAMP(1) AS time;\"";
print "$cmd\n";
system($cmd);

and below is the contab entry

*/2 * * * * /usr/bin/perl /tmp/test.pl

can somebody please help what i am doing wrong ?

  • https://stackoverflow.com/questions/4341324/perl-script-works-but-not-via-cron?rq=1 check this Question. – Ivan Dimitrov Oct 05 '21 at 08:54
  • Crontab runs in own environment. There is a high chance that `perl` is not included into default `PATH`. Looking at your script you could do same with default shell instead of `perl`. – Polar Bear Oct 05 '21 at 16:14
  • @PolarBear - how do i check if perl is included into default path ? below is the bash profile of user which owns the crontab `cat .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/vertica/bin:/opt/vertica/sbin export PATH export LC_ALL="en_US.UTF8" export LANG="en_US.UTF-8" ` – veer kumar rathore Oct 06 '21 at 12:22
  • You can create a cron job with following command `env > /tmp/cronjob.out` and verify what variables are set in this file. – Polar Bear Oct 06 '21 at 18:03

1 Answers1

2

Your output is written to a file called dumpfile_[timestamp].txt. But where is that file?

Your command contains no directory path for that file. So it will be written to the current directory. The current directory for a cronjob is the home directory for the user that owns the cronjob. Have you tried looking there?

It's always better to be more specific about which directory you want the files written to. Two ways to do that are:

  1. Change to the directory before running your command
    */2 * * * * cd /some_directory_path && /usr/bin/perl /tmp/test.pl
  2. Include the full path in your Perl program
    -o /some_directory_path/dumpfile_" . $timenow . ".txt

Update: Ok, take two.

Who owns this cronjob? Any output from the cronjob will be emailed to the owner. And there's definitely output as you have a print() statement. Any errors will be included in the same email. Do you get that email? What's in it?

If you don't get the email, you can change the address that the email is sent to by adding a MAILTO parameter to the crontab. It will look like this:

MAILTO=someone@example.com
*/2 * * * * /usr/bin/perl /tmp/test.pl

Bear in mind that the server might not be set up to send external email, so you might need to use the local mail program on the server.

If you can't get the email to work out, you could look for cron errors in /var/log/syslog (or, on a systemd system, try journalctl _COMM=cron).

The print() output is going somewhere. You need to track it down.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97