简体   繁体   English

从 shell 脚本 ubuntu 在新窗口中运行 Python 脚本

[英]Run a Python Script in a new window from a shell script ubuntu

this may seem like a stupid question.这似乎是一个愚蠢的问题。

I want to run a python script in another command line window from a shell script file which opens all of my programs on one of my servers.我想从一个 shell 脚本文件在另一个命令行窗口中运行 python 脚本,该文件在我的一台服务器上打开我的所有程序。

Currently the python scripts open in the same command line window where the shell script is being ran.目前,python 脚本在运行 shell 脚本的同一命令行窗口中打开。

I've googled around a bit and haven't found anything related to this.我用谷歌搜索了一下,没有找到与此相关的任何内容。

Thanks in advance.提前致谢。

If I understand correctly you have a server, which has a screen attached to it, and you made a shell screen to open a new terminal and run a given set of programs attached to that terminal.如果我理解正确,你有一个服务器,它连接了一个屏幕,你制作了一个 shell 屏幕来打开一个新终端并运行附加到该终端的一组给定程序。

It seems to me that you don't want this in a script but instead should be looking for an init system, such as systemd, which is available in Ubuntu.在我看来,您不希望在脚本中使用它,而是应该寻找一个 init 系统,例如 systemd,它在 Ubuntu 中可用。 Simply put systemd initializes and kills processes running in the background (usually called daemons).简单地说,systemd 会初始化并杀死在后台运行的进程(通常称为守护进程)。

To create a systemd unit file, create a new file at /etc/systemd//system , let's name it example.service , and write the following to it:要创建 systemd 单元文件,请在/etc/systemd//system创建一个新文件,将其命名为example.service ,并将以下内容写入其中:

[Unit]
Description=*Write description here*
Type=simple
Restart=always
RestartSec=1
User=*Write user here*
ExecStart=*Write command you wish to run here*

[Install]
WantedBy=multi-user.target

Now you can start this program in the background with:现在你可以在后台启动这个程序:

$ systemctl daemon-reload
$ systemctl start example

You can also stop it from running:您还可以阻止它运行:

$ systemctl stop example

And even enable it at boot time, so that it persists shutdown/reboot:甚至在启动时启用它,以便它保持关闭/重启:

$ systemctl enable example

You can obtain the output for this program with:您可以通过以下方式获取此程序的输出:

$ systemctl status example

For more information regarding systemd and all of the config options you can either look in the man page with man systemd (very long) or google for a specific issue or feature you wish to implement.有关 systemd 和所有配置选项的更多信息,您可以使用man systemd (很长)或谷歌查看手册页以了解您希望实现的特定问题或功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM