[英]Executing Azure CLI from shell script as cron job
Inside the shell script sing the full path to the Azure CLI & Node JS. 在shell脚本中,单击Azure CLI和Node JS的完整路径。
#!/bin/bash
/opt/nodejs/bin/node /opt/nodejs/bin/azure vm disk list > /tmp/tmpfile
/opt/nodejs/bin/node /opt/nodejs/bin/azure vm disk upload $SRCURL $DESURL <KEY>
When executed manually both the commands are getting executed successfully. 手动执行时,两个命令都会成功执行。 But when executed using crontab vm disk list
command is working but vm disk upload
is not working. 但是当使用crontab执行vm disk list
命令工作但vm disk upload
无效。
Azure JS script requires another cli.js which is been referenced through relative path var AzureCli = require('../lib/cli');
Azure JS脚本需要通过相对路径var AzureCli = require('../lib/cli');
引用的另一个cli.js var AzureCli = require('../lib/cli');
and the cli.js script requires lots of other scripts which is been referenced through relative path. 并且cli.js脚本需要许多其他脚本,这些脚本通过相对路径引用。
Is there any way to provide the same environment profile as the user to cron hence it works or is there any best way to make this work without editing individual JS file and rename the relative path to absolute path? 有没有办法提供与用户相同的环境配置文件,因此它可以工作或有没有任何最好的方法来使这项工作,而无需编辑单独的JS文件并重命名绝对路径的相对路径?
The issue was because the HOME
environment variable was not getting set when executed through cron
and that was causing the Azure command to fail. 问题是因为在通过cron
执行时未设置HOME
环境变量,这导致Azure命令失败。
When the HOME
variable is set to the specific user's home directory in the script, it works fine. 当HOME
变量设置为脚本中特定用户的主目录时,它可以正常工作。
您可以在/ etc / profile文件中的PATH环境变量中设置所需文件的路径
export PATH=$PATH:/path to your files
just redeclare environment variable inside the cron. 只需在cron中重新声明环境变量。
example crontab: 示例crontab:
NODE_ENV=production
* * * * * /bin/sh /path/to/your/shellscript/nodejs
Check for environment variable SHELL=/bin/sh
. 检查环境变量SHELL=/bin/sh
。 Most times /bin/sh
is a symbolic link to /bin/bash
, but on Ubuntu /bin/sh
is a symbolic link to /bin/dash
: 大多数时候/bin/sh
是/bin/bash
的符号链接,但是在Ubuntu /bin/sh
是/bin/dash
的符号链接:
$ ls -la /bin/sh
lrwxrwxrwx 1 root root 4 feb 19 2014 /bin/sh -> dash
So in this case you need to redeclare this environment variable: 因此,在这种情况下,您需要重新声明此环境变量:
export SHELL=/bin/bash
In my case, I export the environment variable in my script and have the following in crontab: 在我的例子中,我在我的脚本中导出环境变量,并在crontab中有以下内容:
# run azure test every 5 minutes
*/5 * * * * /opt/bin/azure-test >/tmp/azure-test.out 2>&1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.