Prakash
Prakash

Reputation: 601

Executing Azure CLI from shell script as cron job

Inside the shell script sing the full path to the 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.

Azure JS script requires another cli.js which is been referenced through relative path var AzureCli = require('../lib/cli'); and the cli.js script requires lots of other scripts which is been referenced through relative path.

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?

Upvotes: 0

Views: 2920

Answers (4)

Jose Beneyto
Jose Beneyto

Reputation: 1

Check for environment variable 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:

$ 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:

# run azure test every 5 minutes
*/5 * * * * /opt/bin/azure-test >/tmp/azure-test.out 2>&1

Upvotes: 0

Francis
Francis

Reputation: 692

just redeclare environment variable inside the cron.

example crontab:

NODE_ENV=production
* * * * * /bin/sh /path/to/your/shellscript/nodejs

Upvotes: 0

Prakash
Prakash

Reputation: 601

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.

When the HOME variable is set to the specific user's home directory in the script, it works fine.

Upvotes: 2

Rahul R Dhobi
Rahul R Dhobi

Reputation: 5806

You can set your path of required file in PATH environment variable in /etc/profile file

export PATH=$PATH:/path to your files

Upvotes: 0

Related Questions