I want to run a shell script on the azure web hosting server while hosting the Django project my Django view contains a subprocess
module code to run the shell script but I don't know which shell is used by my server, bash or something else and I am unable to run that shell script. So How do I run a shell script in the azure web hosting server or any other web hosting server?
I tried using python
import subprocess
sp = subprocess.Popen(['echo $0'], stdout=subprocess.PIPE)
# or
sp = subprocess.Popen(['echo', '$0'], stdout=subprocess.PIPE)
# or
sp = subprocess.Popen(['echo $0'], stdout=subprocess.PIPE, shell=True)
# or
sp = sp = subprocess.Popen(['echo', '$0'], stdout=subprocess.PIPE, shell=True)
x=sp.communicate()
# send response to me or print(x)
to know which bash is used by my hosting server but get nothing. Can I simply use
script.sh
#!/bin/bash
my stuff
CodePudding user response:
As subprocess
module says in it's docs in subprocess_docs :
The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence. On POSIX with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of: Popen(['/bin/sh', '-c', args[0], args1, ...])
So in summary, if you decided to use shell=True
, pass command as a string.
If shell=True
, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user’s home directory.
This can be also helpful: When to use Shell=True for Python subprocess module