1
votes

This is my perl script, it's just a test.

#!/usr/local/bin/perl
open (MYFILE, '>>data.txt');
print MYFILE "Worked!\n";
close (MYFILE);

I saved it as test.pl in cgi-bin/

When I run a shell command,

root@srv ./test.pl it works fine and it adds "Worked!" in the data.txt file successfully.

But when I add this in cronjob to run every minute it doesn't work code:

*/1 * * * * root cd /home/testing/public_html/cgi-bin;./test.pl

I've also tried

*/1 * * * * cd /home/testing/public_html/cgi-bin;./test.pl

I'm thinking it's my environment for cron that's not setup correctly, but I don't know how to fix it.

I've used this in crontab to execute the env

*/1 * * * * env > /home/tmp2/env.cron

And Here is the result:

SHELL=/bin/sh
USER=root
PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
PWD=/root
SHLVL=1
HOME=/root
LOGNAME=root
_=/bin/env

And then I exported the env of the shell i'm using to another file env.shell results:

HOSTNAME=srv.testing.com
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=xxx.xx.xxx.xx 58048 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=0$
MAIL=/var/spool/mail/root
PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
PWD=/home/tmp2
JAVA_HOME=/usr/local/jdk
EDITOR=pico
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LS_OPTIONS=--color=tty -F -a -b -T 0
LOGNAME=root
VISUAL=pico
QTLIB=/usr/lib64/qt-3.3/lib
CVS_RSH=ssh
CLASSPATH=.:/usr/local/jdk/lib/classes.zip
SSH_CONNECTION=xxx.xx.xxx.xx 58048 xx.xx.xx.xx 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/bin/env
OLDPWD=/home/testing/public_html/cgi-bin

I copied the shell path

 PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin

And added it to /etc/crontab and I restart cron but it still didn't work. So, I changed it back to normal, and then I added it to crontab using crontab -e and it still didn't help.

I'm not sure what is going on,

Thank you for your time

2
Start by checking for errors when you handle files: change the second line to open( (MYFILE, '>>', 'data.txt' ) || die "Can't get data.txt: $!"; and run the script again. - Konerak

2 Answers

1
votes

I finally solved it!

I changed the path of crontab -e

PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin

That's the same path in my shell you can get it if you do this root # env > env.shell root # nano env.shell then copy the path from there, and paste it in crontab using crontab -e

Then, the last step was to add it to crontab -e, but after I added the path I had the cronjob in /etc/cron.d/sysstat so that's why it wasn't working.

Now it works and the main problem was the PATH.

Thank you for your time everyone.

0
votes

Try

* * * * * root cd "/home/testing/public_html/cgi-bin"; ./test.pl