简体   繁体   English

在ubuntu服务器上将脚本作为服务运行

[英]Running a script as a service on ubuntu server

I have a python script that I want to run on a remote ubuntu server, Currently I use the SSH using Putty to login to remote server and run the script. 我有一个要在远程ubuntu服务器上运行的python脚本,当前我使用使用Putty的SSH登录到远程服务器并运行该脚本。 But this needs my terminal to be open and needs to have an active internet connection on my local machine until the task is finished. 但这需要我的终端处于打开状态,并且需要在本地计算机上具有活动的Internet连接,直到任务完成。

What I need is to upload the script to server and run it as a service so that I do not need to maintain the active internet connection as well as keeping my terminal open on my local machine. 我需要将脚本上载到服务器并作为服务运行,这样我就不需要维护活动的Internet连接,也不需要在我的本地计算机上保持终端打开。 Once the task is complete I need to login to server and take the result files. 任务完成后,我需要登录服务器并获取结果文件。

How can I do this, server runs Ubuntu Server 16.04 amd64 and local machine runs Windows 10. 如何执行此操作,服务器运行Ubuntu Server 16.04 amd64,本地计算机运行Windows 10。

The quick and dirty approach: 快速而肮脏的方法:

[Putty to the remote host]
$ python your_script.py & disown %1
$ logout

Explanation: The ampersand ( & ) runs the script in the background as a job and control is immediately returned to the shell. 说明:“&”号( & )作为作业在后台运行脚本,并且控件立即返回到Shell。 Then, the disown %1 tells the shell to no longer watch or monitor the job allowing you to logout without killing your process. 然后,被disown %1告诉Shell不再监视或监视作业,从而使您可以注销而不会终止进程。

A more professional approach might use nohup : 一种更专业的方法可以使用nohup

[Putty to the remote host]
$ nohup python your_script.py > /dev/null   # if output can be ignored
$ nohup python your_script.py > output.txt  # if output must be saved
$ logout

If you're talking about an actual service that does not require you to SSH in at all, then take your pick how you solve it -- you might consider writing a SystemD Control file, or installing a cronjob script. 如果您要谈论的是一项完全不需要SSH的实际服务,那么请选择如何解决它-您可以考虑编写SystemD Control文件或安装cronjob脚本。 The latter might be an easier first approach for learning. 后者可能是一种更简单的学习方法。

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

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