Reputation: 43
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
Upvotes: 4
Views: 8963
Reputation: 5257
The answers above helped - but I think the problem is one of these (commented out) variables causes pm2 to silently fail - and not correctly error out. Using the bare bone
pm2 start pm2-server.yaml
pm2 startup #(copy and paste the command given to enable autostart)
pm2-server.yaml
apps:
- name: 'signature'
script: 'server.py'
# instances: '1'
# exec_mode : "cluster"
# wait_ready: true
# autorestart: false
# max_restarts: 0
# max_memory_restart: '150M'
# watch: false
# cron_restart: "*/4 * * * *"
interpreter: "python3"
Upvotes: 0
Reputation: 193
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
.
Upvotes: 12
Reputation: 17367
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.
Upvotes: 0