0

I am trying to run shell (Bash script) running against Azure Linux VM run via Python script. I have managed to run simple commands, but I am not able to run a script.

This is working:

run_command_parameters = {
'command_id': 'RunShellScript',
'script': 'ls -l /tmp'
}]

poller = compute_client.virtual_machines.begin_run_command(resource_group, virtual_machine, run_command_parameters)

result = poller.result()
print(result.value[0].message)

But this is not:

run_command_parameters = {
'command_id': 'RunShellScript',
'script': './my_script.sh'
}]

poller = compute_client.virtual_machines.begin_run_command(resource_group, virtual_machine, run_command_parameters)

result = poller.result()
print(result.value[0].message)

I believe, based on exactly the same PowerShell script the variable called 'script' is used for commands, while other undocumented variable is used to source a whole script. I have tried 'script_path' and not the script is kind of working. While it does not run any command from my_script.sh I am get this:

Enable succeeded:
[stdout]
This is a sample script
Optional parameters: /var/lib/waagent/run-command/download/59/script.sh

[stderr]

Any help? What is the proper way to run a shell script on Azure VMs using Python script? I was trying to find something directly in libraries, but no success.

Best regards, sunrrrise

sunrrrise
  • 1
  • 1
  • maybe you have to use `/full/path/to/script.sh`. or you have to use `bash ./script.sh`. Or you have to add `shebang` in first line of script `#!/usr/bin/bash` and set it executable - `chmod +x script.py` - and then it will treat as program and it will know that it has to use `/usr/bin/bash` to run it. Linux doesn't use extensions (like `.sh`) to recognize how it run script. – furas Jun 07 '22 at 12:58
  • No, unfortunatelly it is not the root cause. I just found out that anything put in `script` variable is being sent as-is to the server. So if I put full path to the script like, say, "C:\Users\sunrrrise\Documents\script.sh" it will be sent and put in `/var/lib/waagent/run-command/{script id}/script.sh` and OS will try to run it and obviously it will fail. In PowerShell there is a variable called `ScriptPath` specifically for that, but I am unable to find anything like that in `_models_py3.py`. – sunrrrise Jun 07 '22 at 13:24

0 Answers0