简体   繁体   English

无法在VPS上运行CRON作业

[英]Trouble getting CRON job running on VPS

I have created a 'sh' script on my VPS which makes copies of the filesystem and SQL and saves them to the same folder which I am going to then push to a backup media. 我已经在VPS上创建了一个'sh'脚本,该脚本可以复制文件系统和SQL,并将它们保存到同一文件夹中,然后将其推入备份媒体。 I know my script is working for this as when I log in over SSH as root and run the command manually it creates a zip file and the SQL backup fine but the CRONjob I have created to execute this script is not working. 我知道我的脚本正在为此工作,因为当我以root用户身份通过​​SSH登录并手动运行命令时,它会创建一个zip文件,并且SQL备份正常,但是我为执行该脚本而创建的CRONjob无法正常工作。 I have created the following cron job in '/etc/crontab': 我在“ / etc / crontab”中创建了以下cron作业:

    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

   # m h dom mon dow user  command
   17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
   4 6 * * *       root    test -x /usr/sbin/anacron || ( cd / && run-    parts --report /etc/cron.daily )
   20 1 * * 7      root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
   51 5 1 * *      root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    * * * * *      root    /home/backup/test.sh 

The 5th entry is the one I have created to test. 第五项是我创建的要测试的项。 The path is correct and I used an absolute path as instructed before. 该路径是正确的,我按照之前的说明使用了绝对路径。 I have written a simple script called test/sh which outputs some text to a file (test.txt) to test the CRON job and it doesn't work. 我编写了一个名为test / sh的简单脚本,该脚本将一些文本输出到文件(test.txt)中以测试CRON作业,但该脚本不起作用。 I am using the command 'tail -f' to monitor changes to the text file and it never gets called. 我正在使用命令“ tail -f”来监视对文本文件的更改,但它从未被调用。 The script works when executed manually though. 该脚本在手动执行时有效。

Here is the simple 'test.sh' file. 这是简单的“ test.sh”文件。 This works correctly when called manually. 手动调用时,此方法可以正常工作。

#!/bin/bash

echo "Dumping at:  $(date)" >> /home/backup/test.txt                                                                                                                                                                        

I understand there may be permissions issues but I thought if I was executing this as 'root' this should be fine? 我了解可能存在权限问题,但我认为如果我以“ root”身份执行此操作,应该可以吗? Can anyone see where I am going wrong? 谁能看到我要去哪里错了?

Thanks 谢谢

So, after a few hours of searching I realised something stupid. 因此,经过几个小时的搜索,我意识到有些愚蠢。 I had left out the 'bash' command from the crontab file. 我从crontab文件中省略了“ bash”命令。

I changed my line to this: 我将行更改为此:

          • root bash /home/backup/test.sh 根bash /home/backup/test.sh

And it is now running. 现在正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM