简体   繁体   English

如何在服务器上运行 python 脚本?

[英]How to run python script on a server?

Well, I am a beginner in python programming language and a backend developer.好吧,我是 python 编程语言的初学者和后端开发人员。 And I have written this python script using Telethon to auto-reply to my messages that are from the Telegram app.我已经使用 Telethon 编写了这个 python 脚本来自动回复来自 Telegram 应用程序的消息。

from telethon import TelegramClient, events
message = "Sorry, I'll be away until next week!"

api_id = 2******
api_hash = 'ffb5**********************'

client = TelegramClient('anon', api_id, api_hash)

@client.on(events.NewMessage)
async def my_event_handler(event):
    if event.is_private:
        await event.respond(message)

client.start()
client.run_until_disconnected()

This is just a simple script and it has no practical help.这只是一个简单的脚本,并没有实际帮助。 I will change it later based on my interests.稍后我会根据我的兴趣更改它。 When I run the script on my computer it is working as long as my computer is working.当我在我的计算机上运行脚本时,只要我的计算机正常工作,它就可以工作。 And I want to run that script always so that it can reply to messages even I am offline.而且我想始终运行该脚本,以便即使我离线也可以回复消息。 And I think I need to run my code on some free servers.而且我认为我需要在一些免费服务器上运行我的代码。

The question:问题:

Can you suggest to me some step by step tutorials or articles that fulfill my interests?您能否向我推荐一些符合我兴趣的分步教程或文章? (Consider that I am absolutely a beginner to server stuff.) (考虑到我绝对是服务器方面的初学者。)

You can just run the process in background.您可以在后台运行该过程。
By running the following for instance (on unix based systems):例如,通过运行以下命令(在基于 unix 的系统上):

python <script_name> &

For a more robust solution that supports auto-restart upon failures, boot, etc.. consider PM2 or Supervisord对于支持在故障、启动等时自动重启的更强大的解决方案。考虑PM2Supervisord

Here is an example command (using the PM2 route)这是一个示例命令(使用 PM2 路由)

pm2 start <script> --interpreter=python

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

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