简体   繁体   English

在AWS EC2实例中设置Cron Job

[英]Setting Cron Job in AWS EC2 instance

I've created a cron job in AWS EC2 but it is not working. 我已经在AWS EC2中创建了一个cron作业,但是它不起作用。

I followed below steps to create cron tab: 我按照以下步骤创建了cron标签:

Step 1: I logged in to AWS EC2 Instace 步骤1:我登录到AWS EC2 Instace

step 2: sudo su 步骤2:sudo su

step 2: crontab -e 步骤2:crontab -e

Step 3: Insert mode 步骤3:插入模式

Step 4: I entered 步骤4:我输入了

    # M H DOM M DOW CMND

15 14 * * 3 /services/backup.sh >/tmp/error.log 2>&1

Step 5: :wq Cron tab is created but not running. 步骤5::wq Cron标签已创建但未运行。

Please can you any one help me , why it is not working? 请谁能帮助我,为什么它不起作用?

UPDATE : 更新:

after lots of researching i found that i can give a log to the particular cron file so added >/tmp/error.log 2>&1 in my file and created error.log there. 经过大量研究后,我发现我可以给特定的cron文件提供日志,因此在文件中添加了> /tmp/error.log 2>&1并在那里创建了error.log。

but i am not getting any cron log there. 但是我在那里没有任何cron日志。 now i think my cron file is not running(obviously it is not running). 现在我认为我的cron文件未运行(显然未运行)。

but how to see whether i am lacking 但是如何看我是否缺乏

and when i checked cron service it is running with the name crond 当我检查cron服务时,它以crond名称运行

service crond status
crond (pid  28283) is running...

(backup.sh script absolutely running fine) (backup.sh脚本绝对运行正常)

could you try full path of backup.sh in cron like below; 您可以像下面这样在cron中尝试backup.sh的完整路径吗? you can use pwd command to find full path of your script. 您可以使用pwd命令查找脚本的完整路径。

# M H DOM M DOW CMND
25 12 * * 3 /home/user/script/backup.sh

Maybe, your cron service not started, you can check status with "ps -ef | grep cron | grep -v grep" 也许您的cron服务未启动,您可以使用“ ps -ef | grep cron | grep -v grep”检查状态

if not see any thing run this; 如果看不到任何东西运行此;

sudo service cron start

if run; 如果运行 change cron as below; 更改cron如下:

15 13 * * 3 root /fullpath/backup.sh >/tmp/error.log 2>&1

then you check error.log 然后你检查error.log

>/tmp/error.log 2>&1 is; > /tmp/error.log 2>&1是; redirect the output of your program to /tmp/error.log. 将程序的输出重定向到/tmp/error.log。 Include both the Standard Error and Standard Out 同时包括标准错误和标准输出

I think your issue might with permission and ownership of your backup.sh file. 我认为您的问题可能与您的backup.sh文件的权限所有权有关。

Check the ownership of file and then do changes accordingly in your /etc/crontab file : 检查文件的所有权,然后在/etc/crontab文件中进行相应的更改:

25 12 * * 3 <owner of the file>  /home/user/script/backup.sh

For example if the owner of file backup.sh is root then it should look something like this: 例如,如果文件backup.sh的所有者是root那么它应该看起来像这样:

25 12 * * 3 root  /home/user/script/backup.sh

Hope this help! 希望对您有所帮助!

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

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