简体   繁体   English

在Crontab中运行NodeJS命令和Linux cmd

[英]Running NodeJS commands and Linux cmd in Crontab

I am running an EC2 AWS instance, I have a range of commands and scripts that run well but will not execute within a crontab. 我正在运行EC2 AWS实例,我有一系列运行良好但不会在crontab中执行的命令和脚本。 Here are the contents of the Crontab: 以下是Crontab的内容:

15 03 * * * pythonscript1.py
45 03 * * * pythonscript2.py
00 05 * * * gsjson 1z5OlqsyU5N2ze1JYHJssfe1LpKNvsr4j8TDGnvyuaDZ gsheet.json
05 05 * * * s3cmd put gsheet.json -P -f s3://mybucket772

Do I need the path of the python program within EC2? 我是否需要EC2中python程序的路径? gsjson was installed as a NPM package using NodeJS and S3cmd is a command line tool I installed. gsjson是使用NodeJS作为NPM软件包安装的,而S3cmd是我安装的命令行工具。

https://s1.postimg.org/nu4zfb22n/ec2-filetree.jpg https://s1.postimg.org/nu4zfb22n/ec2-filetree.jpg

I assume I might the programs path within cron but I'm not sure what command I need to get this. 我想我可能在cron内的程序路径,但我不确定要获取此命令需要什么命令。 Or maybe I should the wrap the last 2 commands in a .js file and point to that? 或者,也许我应该将.js文件中的最后两个命令包装起来并指向它?

使用绝对路径时,无需进一步挖掘设置即可正常工作,即:

15 03 * * * /home/ec2-user/pythonscript1.py

After spending an entire bank holiday wrestling with this. 在度过了整个银行假期后,他为此苦苦挣扎。 Here was the solution, looks so easy now but not when you are wrestling with it: 这是解决方案,现在看起来很简单,但是当您努力解决时却并非如此:

I edited the ec2-user cron using the command: EDITOR=nano crontab -e , whilst logged in as the ec2-user. 我使用以下命令编辑了ec2-user cron: EDITOR=nano crontab -e ,同时以ec2-user登录。 To create my cron job. 创建我的Cron工作。

The main problem I had was not having the following at the top of my crontab 我遇到的主要问题是crontab顶部没有以下内容

PATH=/bin:/usr/local/bin

as these point to the location of gsjson and s3cmd. 因为这些指向gsjson和s3cmd的位置。

However, the Python scripts still wouldn't work as the path for those was usr/bin. 但是,Python脚本仍然无法使用,因为它们的路径是usr / bin。 So we needed the absolute path for both the location of Python and the script. 因此,我们需要Python和脚本位置的绝对路径。 So below was the final cron contents, the working solution. 因此,下面是最终的cron内容,即有效的解决方案。 Thanks for the others input on the question too. 也感谢您对问题的其他投入。

Ensure the scripts are 0755 permissions. 确保脚本具有0755权限。

PATH=/bin:/usr/local/bin

15 03 * * * /usr/bin/python /home/ec2-user/pythonscript1.py
45 03 * * * /usr/bin/python /home/ec2-user/pythonscript2.py
00 05 * * * gsjson 1z5OlqsyU5N2ze1JYHJssfe1LpKNvsr4j8TDGnvyuaDZ gsheet.json
05 05 * * * s3cmd put gsheet.json -P -f s3://mybucket772

Some other useful commands too when configuring: 在配置时,还有一些其他有用的命令:

cat /var/spool/mail/ec2-user 

Enables you to view your error emails within the CLI 使您可以在CLI中查看错误电子邮件

crontab -l 

(Display your crontab file.) (显示您的crontab文件。)

crontab -e 

(Edit your crontab file, or create one if it doesn't already exist) (编辑您的crontab文件,如果不存在则创建一个)

sudo service crond status

(gives current status) (给出当前状态)

which python

(gives your path) (给你的路)

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

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