Cron 作业不执行 perl 脚本

发布于 2025-01-04 10:30:16 字数 2377 浏览 1 评论 0原文

这是我的 perl 脚本,这只是一个测试。

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

我将其保存为 cgi-bin/ 中的 test.pl

当我运行 shell 命令时,

root@srv ./test.pl 它工作正常,并且添加了“工作正常!”在 data.txt 文件中成功。

但是当我将其添加到 cronjob 中以每分钟运行一次时,它不起作用 代码:

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

我也尝试过

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

,我认为这是我的 cron 环境设置不正确,但我不知道如何修复它。

我在 crontab 中使用它来执行 env

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

这是结果:

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

然后我将我正在使用的 shell 的 env 导出到另一个文件 env.shell 结果:

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

我复制了 shell 路径

 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

并将其添加到 /etc/crontab 中,然后重新启动 cron 但它仍然不起作用。所以,我把它改回正常,然后我使用 crontab -e 将其添加到 crontab 中,但仍然没有帮助。

我不确定发生了什么,

谢谢您的宝贵时间

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是你 2025-01-11 10:30:16

我终于解决了!

我更改了 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

,这与我的 shell 中的路径相同,如果执行此操作,您可以获得它
根#env>环境外壳
root#nano env.shell
然后从那里复制路径,并使用 crontab -e 将其粘贴到 crontab 中

然后,最后一步是将其添加到 crontab -e 中,但在添加路径后,我在 /etc/cron.d/sysstat 中有 cronjob ,所以这就是它不起作用的原因。

现在它可以工作了,主要问题是路径。

感谢大家抽出宝贵的时间。

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.

南街九尾狐 2025-01-11 10:30:16

尝试

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

Try

* * * * * root cd "/home/testing/public_html/cgi-bin"; ./test.pl
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文