我试着在谷歌上搜索答案,但一无所获。
我需要使用我的作品超级计算机服务器,但我的python脚本运行,它必须通过一个shell脚本执行。
例如,我希望job.sh执行python_script.py
如何才能做到这一点呢?
我试着在谷歌上搜索答案,但一无所获。
我需要使用我的作品超级计算机服务器,但我的python脚本运行,它必须通过一个shell脚本执行。
例如,我希望job.sh执行python_script.py
如何才能做到这一点呢?
当前回答
恕我直言,写作
python /path/to/script.py
是不对的,尤其是在现在。python ?python2.6吗?2.7 ?3.0 ?3.1 ?大多数情况下,您需要在python文件的shebang标签中指定python版本。我鼓励使用#!/usr/bin/env python2 #或python2.6或python3,甚至python3.1兼容性。
在这种情况下,让脚本可执行并直接调用会更好:
#!/bin/bash /path/to/script.py
这样,你需要的python版本只写在一个文件中。现在大多数系统都同时使用python2和python3,而且符号链接python指向python3,而大多数人都希望它指向python2。
其他回答
方法1 -创建一个shell脚本:
假设你有一个python文件hello.py 创建一个名为job.sh的文件,其中包含
#!/bin/bash
python hello.py
将其标记为可执行
$ chmod +x job.sh
然后运行它
$ ./job.sh
方法2 (BETTER) -让python本身从shell运行:
修改脚本hello.py,并将其添加为第一行
#!/usr/bin/env python
将其标记为可执行
$ chmod +x hello.py
然后运行它
$ ./hello.py
恕我直言,写作
python /path/to/script.py
是不对的,尤其是在现在。python ?python2.6吗?2.7 ?3.0 ?3.1 ?大多数情况下,您需要在python文件的shebang标签中指定python版本。我鼓励使用#!/usr/bin/env python2 #或python2.6或python3,甚至python3.1兼容性。
在这种情况下,让脚本可执行并直接调用会更好:
#!/bin/bash /path/to/script.py
这样,你需要的python版本只写在一个文件中。现在大多数系统都同时使用python2和python3,而且符号链接python指向python3,而大多数人都希望它指向python2。
这里我演示了一个在shell脚本中运行python脚本的示例。出于不同的目的,您可能需要从shell命令中读取输出,在同一个文件中执行python脚本和shell命令。
使用os.system()方法从python中执行shell命令。要从shell命令读取输出,请使用os.popen()。
下面是一个示例,它将grep所有包含文本sample_program.py的进程。然后在收集了进程id(使用python)之后,它会将它们全部杀死。
#!/usr/bin/python3
import os
# listing all matched processes and taking the output into a variable s
s = os.popen("ps aux | grep 'sample_program.py'").read()
s = '\n'.join([l for l in s.split('\n') if "grep" not in l]) # avoiding killing the grep itself
print("To be killed:")
print(s)
# now manipulating this string s and finding the process IDs and killing them
os.system("kill -9 " + ' '.join([x.split()[1] for x in s.split('\n') if x]))
引用:
在shell脚本中执行python程序 指定os的输出。系统的一个变量,并防止它显示在屏幕上
这对我来说很管用:
创建一个新的shell文件作业。所以我们说: touch job.sh和添加命令来运行python脚本(你甚至可以向python添加命令行参数,我通常预定义我的命令行参数)。 Chmod +x job.sh 在job.sh中添加以下py文件,假设: python_file.py参数1参数2参数3 >> test -output.txt && echo "Done with python_file.py" testpy-output.txt && echo "Done with python_file1.py"
Output of job.sh should look like this:使用python_file.py完成
使用python_file1.py完成
我通常在必须运行多个具有不同预定义参数的python文件时使用这种方法。
注意:只是快速提醒一下这里发生了什么:
python_file.py argument1 argument2 argument3 >> testpy-output.txt && echo "completed with python_file.py" .
Here shell script will run the file python_file.py and add multiple command-line arguments at run time to the python file. This does not necessarily means, you have to pass command line arguments as well. You can just use it like: python python_file.py, plain and simple. Next up, the >> will print and store the output of this .py file in the testpy-output.txt file. && is a logical operator that will run only after the above is executed successfully and as an optional echo "completed with python_file.py" will be echoed on to your cli/terminal at run time.
你应该能够调用它作为python scriptname.py。
# !/bin/bash
python /home/user/scriptname.py
还要确保脚本具有运行权限。
您可以使用chmod u+x scriptname.py使其可执行。