0

I have a python program that needs to run a bash command on a server. The program works when I run a bash command in my local directory like this:

import subprocess from subprocess import call call(['bash', 'Script_Test.sh'])

However, after SSH'ing to a server and running a similar line of code below with a path on the server to a bash script, I get the error "No such file or directory"

call['bash', path]

This doesn't make sense for a number of reasons. I triple checked that the path is correct. I went on Putty, connected to the server on there, and ran a bash command with the same path and it worked, so it can't be the path. I also ran a number of tests to make sure I was SSH'd into the correct server, and I was. I thought there was a security issue on the server with me running bash, so I tried cat instead. Nope, still unable to locate the path.

I'm not super familiar with python subprocesses, so any pointers to anything I'm missing here with "call" would be very helpful.

Ryan Williams
  • 119
  • 2
  • 8
  • Add lines `import os.path; print (os.path.isfile(path))` to your python code. If your program returns False, it means that the file: `path` doesn't exist. So, you need to relocate the bash script file to its proper location. Also, test if you have bash and python installed on your server by using `which bash` and `which python`. – Saeid BK Jul 06 '16 at 19:02
  • You might be getting the error "No such file or directory" from the subprocess running your Script_Test.sh. It might be related to the fact that your subprocess' path might differ from where your script_test.sh resides. You can check the subprocess' path by printing from inside of it using `pwd`. – smido Jul 18 '17 at 15:06

1 Answers1

2

Making Sure Your Script is Ready for Execution

  1. Give your script a shebang line

First things first, it is important that you include a shebang line in your script on Unix-like systems. I recommend, for your script's portability, that you use #!/usr/bin/env bash.

A Note on File Extensions:

I would recommend that you remove the .sh extension from your script. If you are using the Bourne Again Shell (bash) to execute your script, then using the .sh extension is misleading. Simply put, the Bourne Shell (sh) is different than the Bourne Again Shell (bash) - so don't use a file extension that suggests you are using a different shell than you actually are!

It's not the end of the world if you don't do change your file extension - your script will still be executed as a bash script if you have the proper bash shebang line. Still, it is good practice to either use no file extension (Script_Test -- strongly preferred) or the .bash file extension (Script_Test.bash).

  1. Give your script the proper file permissions

For your purposes, maybe it is only important to give the current user permissions to read and execute the script. In that case, use chmod u+x Script_Test.sh. What is important here is that the correct user (u+) / group (g+) has permissions to execute the script.

  1. Make sure that your script's path is in the $PATH environment variable

Executing your bash script in a Python script

Once you've followed these steps, your Python script should work as you've called it in your question:

import subprocess
from subprocess import call

your_call = call("Test_Script.sh")

If you would rather not move your script into the $PATH environment variable, just make sure that you refer to the script's full path (which is the current directory, ./, in your case):

import subprocess
from subprocess import call

your_call = call("./Test_Script.sh")

Lastly, if your script does not have a shebang line, you will need to specify an additional parameter in your call function:

import subprocess
from subprocess import call

your_call = call("./Test_Script.sh", shell=True)

However, I would not recommend this last approach. See the Python 2.7.12 documentation for the subprocess package...

Warning: Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.

Please check out @zenpoy's explanation to a similar StackOverflow question if you are still having problems.

Happy coding!

Community
  • 1
  • 1
Vladislav Martin
  • 1,504
  • 2
  • 15
  • 34
  • Perhaps see also [Actual meaning of `shell=True` in subprocess](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) for some elaboration around the warning about the security implications. – tripleee Dec 13 '22 at 08:06