简体   繁体   中英

Running a python script with pm2 error

First of all, sorry if this is a duplicate, i've searched, found some fixes but i'm dumb and could not apply. I'm not a developer, programmer, just trying to run a python script that does some things .I've got basic understanding of python so speak to me like i'm dumb.

These are the dependencies that makes the bot run . Running python3 bot.py runs ok, but i need some thingy that restarts the program when it dies, and pm2 seems easy for a moron like me.

import discord
import asyncio
import datetime
import os
import json
import requests
from coinmarketcap import Market
from steem import Steem
from steem.post import Post
from steem.blog import Blog
from steem.instance import set_shared_steemd_instance
from steem.account import Account
from steem.steemd import Steemd
from discord.ext.commands import Bot
from discord.ext import commands

When i run pm2 start bot.py This is what i get pm2 ls ┌──────┬──────┬─────────┬────┬─────┬────────┐ │ Name │ mode │ status │ ↺ │ cpu │ memory │ ├──────┼──────┼─────────┼────┼─────┼────────┤ │ bot │ fork │ errored │ 15 │ 0% │ 0 B

I've read that pm2 should identify the .py and run the script, so why isn't it working like it's supposed to?

pm2 -v 2.10.1 Running ubuntu 16.04 LTS

Since I haven't really seen my solution for this problem, and there was no solution verified, I'll give mine. Hope it helps!

While it is true pm2 is made for Node.js, it is possible to run Python scripts. I've been doing it for a while, and it's quite alright!

I also read pm2 should be auto-detecting the .py in your file, but to my experience it doesn't do a good job at detecting python3. Specifying which interpreter to use helps in my case.

You do this by adding following parameter: --interpreter python3 . So in your case the full command would be pm2 start bot.py --interpreter python3 .

To the best of my knowledge (which I admit is not comprehensive), pm2 is a process runner/monitor/manager which was developed specifically for node.js scripts.

While it does seem possible to manage python processes using pm2 I have never heard of anyone doing so.

To your question, you might discover the reason for your failure by checking out the contents of ~/.pm2/logs which is where pm2 logs its runtime information. (The ~ above references your HOME directory.)

Since you can run the script by itself, I think you might be hitting a file path issue. To triage, create a script called pm2-test.js with:

console.log(JSON.stringify(process.env, null, 2))

Run this using pm2 ./pm2-test.js --name "test"

And then run pm2 log test which will print the output log of pm2-test to the console. (You'll need to CTRL-C to stop this because it is designed to continually stream new log entries as they appear.)

Pay specific attention to any errors related to PATH or containing text like cannot locate or not found .

Remember to pm2 delete test to remove the process from pm2's management.

If these two logs don't give you enough information to fix the problem, you might try using another process monitor.

There are several process monitors written in python which can be found via Google.

As I have no experience with any of them and I tend to prefer tools written specifically for the problem at hand, I'd recommend using monit which is a general process manager/monitor that is easy to set-up, very stable, and for which there are scores of example configurations available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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